diff --git a/CHANGES.rst b/CHANGES.rst index 15a480dda..791dce468 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,11 +7,13 @@ Version 0.15.dev0 Bug fixes: * Fix tempo handling of multi-track MIDI files (#219) +* Fix error loading unicode filenames (#223) Other changes: * `num_threads` is passed to `ParallelProcessor` in single mode (#217) -* use `install_requires` in `setup.py` to specify dependencies (#226) +* Use `install_requires` in `setup.py` to specify dependencies (#226) + Version 0.14.1 (release date: 2016-08-01) ----------------------------------------- diff --git a/madmom/audio/signal.py b/madmom/audio/signal.py index 1d10c6713..08dc71da0 100644 --- a/madmom/audio/signal.py +++ b/madmom/audio/signal.py @@ -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: diff --git a/madmom/processors.py b/madmom/processors.py index ce5fc5b98..966481af9 100644 --- a/madmom/processors.py +++ b/madmom/processors.py @@ -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 @@ -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) diff --git a/tests/test_audio_signal.py b/tests/test_audio_signal.py index dcabe5359..6e269ef4d 100644 --- a/tests/test_audio_signal.py +++ b/tests/test_audio_signal.py @@ -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 @@ -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 diff --git a/tests/test_processors.py b/tests/test_processors.py new file mode 100644 index 000000000..2a3dfd82a --- /dev/null +++ b/tests/test_processors.py @@ -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)