Ticket #16563: ticket_16563.diff

File ticket_16563.diff, 1.6 KB (added by Luke Plant, 13 years ago)

Patch with test

  • django/utils/functional.py

    diff -r b33d4705456a django/utils/functional.py
    a b  
    11import copy
    22import operator
     3import pickle
    34from functools import wraps, update_wrapper
    45
    56
     
    261262        else:
    262263            return copy.deepcopy(self._wrapped, memo)
    263264
     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
    264271    # Need to pretend to be the wrapped class, for the sake of objects that care
    265272    # about this (especially in equality tests)
    266273    __class__ = property(new_method_proxy(operator.attrgetter("__class__")))
  • tests/regressiontests/utils/simplelazyobject.py

    diff -r b33d4705456a tests/regressiontests/utils/simplelazyobject.py
    a b  
    11import copy
     2import pickle
    23
    34from django.utils.unittest import TestCase
    45from django.utils.functional import SimpleLazyObject, empty
     
    9697        self.assertTrue(x)
    9798        x = SimpleLazyObject(lambda: 0)
    9899        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)
Back to Top