Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes error loading unicode filenames #223

Merged
merged 1 commit into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
-----------------------------------------
Expand Down
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))
Copy link
Collaborator

@superbock superbock Oct 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to check this only for Python 2? Shouldn't this be checked for Python 3, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 3 has no unicode type, therefore I cannot execute unicode(sample_file).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Python 3, all strings are unicode by default.


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)