Permalink
Browse files

[1.11.x] Fixed #28389 -- Fixed pickling of LazyObject on Python 2 whe…

…n wrapped object doesn't have __reduce__().

Partial revert of 35355a4.
  • Loading branch information...
sir-sigurd authored and timgraham committed Jul 12, 2017
1 parent fe7b456 commit 30f334cc58e939c7d9bd8455c80bd066fbde9f2b
Showing with 12 additions and 6 deletions.
  1. +7 −6 django/utils/functional.py
  2. +3 −0 docs/releases/1.11.4.txt
  3. +2 −0 tests/utils_tests/test_lazyobject.py
View
@@ -300,13 +300,14 @@ def __reduce__(self):
self._setup()
return (unpickle_lazyobject, (self._wrapped,))
+ # Overriding __class__ stops __reduce__ from being called on Python 2.
+ # So, define __getstate__ in a way that cooperates with the way that
+ # pickle interprets this class. This fails when the wrapped class is a
+ # builtin, but it's better than nothing.
def __getstate__(self):
- """
- Prevent older versions of pickle from trying to pickle the __dict__
- (which in the case of a SimpleLazyObject may contain a lambda). The
- value will be ignored by __reduce__() and the custom unpickler.
- """
- return {}
+ if self._wrapped is empty:
+ self._setup()
+ return self._wrapped.__dict__
def __copy__(self):
if self._wrapped is empty:
View
@@ -15,3 +15,6 @@ Bugfixes
* Fixed ``QuerySet.union()`` and ``difference()`` when combining with
a queryset raising ``EmptyResultSet`` (:ticket:`28378`).
+
+* Fixed a regression in pickling of ``LazyObject`` on Python 2 when the wrapped
+ object doesn't have ``__reduce__()`` (:ticket:`28389`).
@@ -187,11 +187,13 @@ def __iter__(self):
def test_pickle(self):
# See ticket #16563
obj = self.lazy_wrap(Foo())
+ obj.bar = 'baz'
pickled = pickle.dumps(obj)
unpickled = pickle.loads(pickled)
self.assertIsInstance(unpickled, Foo)
self.assertEqual(unpickled, obj)
self.assertEqual(unpickled.foo, obj.foo)
+ self.assertEqual(unpickled.bar, obj.bar)
# Test copying lazy objects wrapping both builtin types and user-defined
# classes since a lot of the relevant code does __dict__ manipulation and

0 comments on commit 30f334c

Please sign in to comment.