Ticket #12049: lazyobject-deepcopy.diff

File lazyobject-deepcopy.diff, 2.2 KB (added by Christian Hammond, 15 years ago)

Implements deepcopy for LazyObject

  • django/utils/functional.py

     
    307307        if self._wrapped is None: self._setup()
    308308        return str(self._wrapped)
    309309
     310    def __deepcopy__(self, memo):
     311        if self._wrapped is None:
     312            result = self.__class__(self._setupfunc)
     313            memo[id(self)] = result
     314            return result
     315        else:
     316            import copy
     317            return copy.deepcopy(self._wrapped, memo)
     318
    310319    def _setup(self):
    311320        self._wrapped = self._setupfunc()
  • tests/regressiontests/context_processors/tests.py

     
    33"""
    44
    55from django.conf import settings
     6from django.contrib.auth.models import Group
     7from django.db.models import Q
    68from django.test import TestCase
    79from django.template import Template
    810
     
    8183        self.assertContains(response, "username: super")
    8284        # bug #12037 is tested by the {% url %} in the template:
    8385        self.assertContains(response, "url: /userpage/super/")
     86
     87        # See if this object can be used for queries where a Q() comparing
     88        # a user can be used with another Q() (in an AND or OR fashion).
     89        # This simulates what a template tag might do with the user from the
     90        # context. Note that we don't need to execute a query, just build it.
     91        #
     92        # The failure case (bug #12049) on Python 2.4 with a LazyObject-wrapped
     93        # User is a fatal TypeError: "function() takes at least 2 arguments
     94        # (0 given)" deep inside deepcopy().
     95        #
     96        # Python 2.5 and 2.6 succeeded, but logged internally caught exception
     97        # spew:
     98        #
     99        #    Exception RuntimeError: 'maximum recursion depth exceeded while
     100        #    calling a Python object' in <type 'exceptions.AttributeError'>
     101        #    ignored"
     102        query = Q(user=response.context['user']) & Q(someflag=True)
Back to Top