From 4ef285de22697c53edb27de3b0e13eae5ddbc5a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6ck?= Date: Tue, 8 Mar 2016 14:37:31 +0100 Subject: [PATCH] make (un-)pickling of Processors work across Python versions warn, if the loaded Processor has not the same class as the dumped one --- madmom/processors.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/madmom/processors.py b/madmom/processors.py index 299398192..5a48ce7e1 100644 --- a/madmom/processors.py +++ b/madmom/processors.py @@ -29,8 +29,8 @@ class Processor(object): """ - @staticmethod - def load(infile): + @classmethod + def load(cls, infile): """ Instantiate a new Processor from a file. @@ -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): """ @@ -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): """