Skip to content

Commit

Permalink
make (un-)pickling of Processors work across Python versions
Browse files Browse the repository at this point in the history
warn, if the loaded Processor has not the same class as the dumped one
  • Loading branch information
Sebastian Böck committed Mar 8, 2016
1 parent 411db6b commit 4ef285d
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions madmom/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class Processor(object):
"""

@staticmethod
def load(infile):
@classmethod
def load(cls, infile):
"""
Instantiate a new Processor from a file.
Expand All @@ -55,7 +55,21 @@ def load(infile):
infile.close()
infile = infile.name
# instantiate a new Processor and return it
return pickle.load(open(infile, 'rb'))
with open(infile, 'rb') as f:
# Python 2 and 3 behave differently
try:
# Python 3
obj = pickle.load(f, encoding='latin1')
except TypeError:
# Python 2 doesn't have/need the encoding
obj = pickle.load(f)
# warn if the unpickled Processor is of other type
if obj.__class__ is not cls:
import warnings
warnings.warn("Expected Processor of class '%s' but loaded "
"Processor is of class '%s', processing anyways." %
(cls.__name__, obj.__class__.__name__))
return obj

def dump(self, outfile):
"""
Expand All @@ -72,16 +86,13 @@ def dump(self, outfile):
"""
import pickle
import warnings
warnings.warn('The resulting file is considered a model file, please '
'see the LICENSE file for details!')
# close the open file if needed and use its name
if not isinstance(outfile, str):
outfile.close()
outfile = outfile.name
# dump the Processor to the given file
pickle.dump(self, open(outfile, 'wb'),
protocol=pickle.HIGHEST_PROTOCOL)
# Note: for Python 2 / 3 compatibility reason use protocol 2
pickle.dump(self, open(outfile, 'wb'), protocol=2)

def process(self, data):
"""
Expand Down

0 comments on commit 4ef285d

Please sign in to comment.