Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16563 - Error pickling request.user

Thanks to zero.fuxor for the report

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17202 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 655b29b5ba6200560e69bcdcfae5e2e892bddd9c 1 parent 46c1d15
Luke Plant authored December 15, 2011
10  django/utils/functional.py
@@ -261,6 +261,16 @@ def __deepcopy__(self, memo):
261 261
         else:
262 262
             return copy.deepcopy(self._wrapped, memo)
263 263
 
  264
+    # Because we have messed with __class__ below, we confuse pickle as to what
  265
+    # class we are pickling. It also appears to stop __reduce__ from being
  266
+    # called. So, we define __getstate__ in a way that cooperates with the way
  267
+    # that pickle interprets this class.  This fails when the wrapped class is a
  268
+    # builtin, but it is better than nothing.
  269
+    def __getstate__(self):
  270
+        if self._wrapped is empty:
  271
+            self._setup()
  272
+        return self._wrapped.__dict__
  273
+
264 274
     # Need to pretend to be the wrapped class, for the sake of objects that care
265 275
     # about this (especially in equality tests)
266 276
     __class__ = property(new_method_proxy(operator.attrgetter("__class__")))
10  tests/regressiontests/utils/simplelazyobject.py
... ...
@@ -1,4 +1,5 @@
1 1
 import copy
  2
+import pickle
2 3
 
3 4
 from django.utils.unittest import TestCase
4 5
 from django.utils.functional import SimpleLazyObject, empty
@@ -96,3 +97,12 @@ def test_bool(self):
96 97
         self.assertTrue(x)
97 98
         x = SimpleLazyObject(lambda: 0)
98 99
         self.assertFalse(x)
  100
+
  101
+    def test_pickle_complex(self):
  102
+        # See ticket #16563
  103
+        x = SimpleLazyObject(complex_object)
  104
+        pickled = pickle.dumps(x)
  105
+        unpickled = pickle.loads(pickled)
  106
+        self.assertEqual(unpickled, x)
  107
+        self.assertEqual(unicode(unpickled), unicode(x))
  108
+        self.assertEqual(unpickled.name, x.name)

0 notes on commit 655b29b

Please sign in to comment.
Something went wrong with that request. Please try again.