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 committed Oct 25, 2016
1 parent 1765017 commit 4111bdc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 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
4 changes: 3 additions & 1 deletion 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
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

0 comments on commit 4111bdc

Please sign in to comment.