diff -r b33d4705456a django/utils/functional.py
a
|
b
|
|
1 | 1 | import copy |
2 | 2 | import operator |
| 3 | import pickle |
3 | 4 | from functools import wraps, update_wrapper |
4 | 5 | |
5 | 6 | |
… |
… |
|
261 | 262 | else: |
262 | 263 | return copy.deepcopy(self._wrapped, memo) |
263 | 264 | |
| 265 | def __getstate__(self): |
| 266 | # We can't pickle the callable setupfunc, so avoid it. |
| 267 | if self._wrapped is empty: |
| 268 | self._setup() |
| 269 | return {'_wrapped': self._wrapped} |
| 270 | |
264 | 271 | # Need to pretend to be the wrapped class, for the sake of objects that care |
265 | 272 | # about this (especially in equality tests) |
266 | 273 | __class__ = property(new_method_proxy(operator.attrgetter("__class__"))) |
diff -r b33d4705456a tests/regressiontests/utils/simplelazyobject.py
a
|
b
|
|
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 | 97 | self.assertTrue(x) |
97 | 98 | x = SimpleLazyObject(lambda: 0) |
98 | 99 | self.assertFalse(x) |
| 100 | |
| 101 | def test_pickle(self): |
| 102 | # See ticket #16563 |
| 103 | from django.contrib.auth.models import AnonymousUser |
| 104 | x = SimpleLazyObject(lambda: AnonymousUser()) |
| 105 | pickled = pickle.dumps(x) |
| 106 | unpickled = pickle.loads(pickled) |
| 107 | self.assertTrue(unpickled.is_anonymous()) |
| 108 | self.assertEqual(x, unpickled) |