Properly support pickling of LazyObjects in Python 3.3

This commit is contained in:
Ian Clelland 2012-09-28 12:57:38 -07:00 committed by Luke Plant
parent bdcd2f6b10
commit 3266c26eb2
1 changed files with 10 additions and 0 deletions

View File

@ -293,6 +293,16 @@ class SimpleLazyObject(LazyObject):
self._setup() self._setup()
return self._wrapped.__dict__ return self._wrapped.__dict__
# Python 3.3 will call __reduce__ when pickling; these methods are needed
# to serialize and deserialize correctly. They are not called in earlier
# versions of Python.
@classmethod
def __newobj__(cls, *args):
return cls.__new__(cls, *args)
def __reduce__(self):
return (self.__newobj__, (self.__class__,), self.__getstate__())
# Need to pretend to be the wrapped class, for the sake of objects that care # Need to pretend to be the wrapped class, for the sake of objects that care
# about this (especially in equality tests) # about this (especially in equality tests)
__class__ = property(new_method_proxy(operator.attrgetter("__class__"))) __class__ = property(new_method_proxy(operator.attrgetter("__class__")))