Skip to content

Commit

Permalink
fixes error loading unicode filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krebs authored and Sebastian Böck committed Nov 30, 2016
1 parent c818076 commit abadca3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
4 changes: 3 additions & 1 deletion madmom/audio/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,13 @@ def load_audio_file(filename, sample_rate=None, num_channels=None, start=None,
from .ffmpeg import load_ffmpeg_file

# determine the name of the file if it is a file handle
if not isinstance(filename, str):
try:
# close the file handle if it is open
filename.close()
# use the file name
filename = filename.name
except AttributeError:
pass
# try reading as a wave file
error = "All attempts to load audio file %r failed." % filename
try:
Expand Down
8 changes: 6 additions & 2 deletions madmom/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ def load(cls, infile):
"""
import pickle
# close the open file if needed and use its name
if not isinstance(infile, str):
try:
infile.close()
infile = infile.name
except AttributeError:
pass
# instantiate a new Processor and return it
with open(infile, 'rb') as f:
# Python 2 and 3 behave differently
Expand Down Expand Up @@ -87,9 +89,11 @@ def dump(self, outfile):
"""
import pickle
# close the open file if needed and use its name
if not isinstance(outfile, str):
try:
outfile.close()
outfile = outfile.name
except AttributeError:
pass
# dump the Processor to the given file
# Note: for Python 2 / 3 compatibility reason use protocol 2
pickle.dump(self, open(outfile, 'wb'), protocol=2)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_audio_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import tempfile
import unittest
from os.path import join as pj
import sys

from . import AUDIO_PATH, DATA_PATH
from .test_audio_comb_filters import sig_1d, sig_2d
Expand Down Expand Up @@ -723,6 +724,9 @@ def test_types(self):
self.assertIsInstance(signal, np.ndarray)
self.assertTrue(signal.dtype == np.int16)
self.assertTrue(type(sample_rate) == int)
if sys.version_info[0] == 2:
# test unicode string type (Python 2 only)
signal, sample_rate = load_audio_file(unicode(sample_file))

def test_file_handle(self):
# test wave loader
Expand Down
32 changes: 32 additions & 0 deletions tests/test_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# encoding: utf-8
# pylint: skip-file
"""
This file contains tests for the madmom.processors module.
"""

from __future__ import absolute_import, division, print_function
import tempfile
import unittest
import sys

from madmom.models import *
from madmom.ml.nn import NeuralNetwork

tmp_file = tempfile.NamedTemporaryFile(delete=False).name


class TestProcessor(unittest.TestCase):

def test_unicode(self):
if sys.version_info[0] == 2:
# load from unicode string
rnn = NeuralNetwork.load(unicode(ONSETS_RNN[0]))
# save to unicode string
rnn.dump(unicode(tmp_file))


# clean up
def teardown():
import os
os.unlink(tmp_file)

0 comments on commit abadca3

Please sign in to comment.