6
6
from django.utils import six
7
from django.utils.six.moves import copyreg
8
10
# You can't trivially replace this `functools.partial` because this binds to
9
11
# classes and returns bound instances, whereas functools.partial (on CPython)
295
297
return self._wrapped.__dict__
297
# Python 3.3 will call __reduce__ when pickling; these methods are needed
298
# to serialize and deserialize correctly. They are not called in earlier
299
# versions of Python.
299
# Python 3.3 will call __reduce__ when pickling; this method is needed
300
# to serialize and deserialize correctly.
301
302
def __newobj__(cls, *args):
302
303
return cls.__new__(cls, *args)
304
def __reduce__(self):
305
return (self.__newobj__, (self.__class__,), self.__getstate__())
305
def __reduce_ex__(self, proto):
307
# On Py3, since the default protocol is 3, pickle uses the
308
# ``__newobj__`` method (& more efficient opcodes) for writing.
309
return (self.__newobj__, (self.__class__,), self.__getstate__())
311
# On Py2, the default protocol is 0 (for back-compat) & the above
312
# code fails miserably (see regression test). Instead, we return
313
# exactly what's returned if there's no ``__reduce__`` method at
315
return (copyreg._reconstructor, (self.__class__, object, None), self.__getstate__())
307
317
# Need to pretend to be the wrapped class, for the sake of objects that care
308
318
# about this (especially in equality tests)