Commits

Luke Plant  committed cea1cf4

Fixed #16563 - Error pickling request.user

Thanks to zero.fuxor for the report

  • Participants
  • Parent commits 4cafe17

Comments (0)

Files changed (2)

File django/utils/functional.py

         else:
             return copy.deepcopy(self._wrapped, memo)
 
+    # Because we have messed with __class__ below, we confuse pickle as to what
+    # class we are pickling. It also appears to stop __reduce__ from being
+    # called. So, we 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 is better than nothing.
+    def __getstate__(self):
+        if self._wrapped is empty:
+            self._setup()
+        return self._wrapped.__dict__
+
     # Need to pretend to be the wrapped class, for the sake of objects that care
     # about this (especially in equality tests)
     __class__ = property(new_method_proxy(operator.attrgetter("__class__")))

File tests/regressiontests/utils/simplelazyobject.py

 import copy
+import pickle
 
 from django.utils.unittest import TestCase
 from django.utils.functional import SimpleLazyObject, empty
         self.assertTrue(x)
         x = SimpleLazyObject(lambda: 0)
         self.assertFalse(x)
+
+    def test_pickle_complex(self):
+        # See ticket #16563
+        x = SimpleLazyObject(complex_object)
+        pickled = pickle.dumps(x)
+        unpickled = pickle.loads(pickled)
+        self.assertEqual(unpickled, x)
+        self.assertEqual(unicode(unpickled), unicode(x))
+        self.assertEqual(unpickled.name, x.name)