import pickle
from io import StringIO
[docs]class AliasUnpickler(pickle.Unpickler):
[docs] def __init__(self, aliases, *args, **kw):
pickle.Unpickler.__init__(self, *args, **kw)
self.aliases = aliases
[docs] def find_class(self, module, name):
module, name = self.aliases.get((module, name), (module, name))
return pickle.Unpickler.find_class(self, module, name)
[docs]class AliasPickleModule:
[docs] def __init__(self, aliases):
self.aliases = aliases
[docs] def dump(self, obj, fileobj, protocol=0):
return pickle.dump(obj, fileobj, protocol)
[docs] def dumps(self, obj, protocol=0):
return pickle.dumps(obj, protocol)
[docs] def load(self, fileobj):
return AliasUnpickler(self.aliases, fileobj).load()
[docs] def loads(self, string):
fileobj = StringIO(string)
return AliasUnpickler(self.aliases, fileobj).load()