summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorLuke Plant <L.Plant.98@cantab.net>2009-10-15 14:12:34 +0000
committerLuke Plant <L.Plant.98@cantab.net>2009-10-15 14:12:34 +0000
commita2d8acbacda353248fd191b97155cd4cf27dcd66 (patch)
tree5941baafeb1c30840e837043418e9a362a83000e /django
parentc161bf21f0ab9f2945ae7a44e5757b071f7eb712 (diff)
Fixed a regression on Python 2.6 caused by r11623
This might fix #12037, but I cannot reproduce that bug. Refs #12037 git-svn-id: http://code.djangoproject.com/svn/django/trunk@11625 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/core/context_processors.py24
-rw-r--r--django/utils/functional.py28
2 files changed, 27 insertions, 25 deletions
diff --git a/django/core/context_processors.py b/django/core/context_processors.py
index 707068d3be..0dbd8449b6 100644
--- a/django/core/context_processors.py
+++ b/django/core/context_processors.py
@@ -8,27 +8,7 @@ RequestContext.
"""
from django.conf import settings
-from django.utils.functional import lazy, memoize, LazyObject
-
-class ContextLazyObject(LazyObject):
- """
- A lazy object initialised from any function, useful for lazily
- adding things to the Context.
-
- Designed for compound objects of unknown type. For simple objects of known
- type, use django.utils.functional.lazy.
- """
- def __init__(self, func):
- """
- Pass in a callable that returns the actual value to be used
- """
- self.__dict__['_setupfunc'] = func
- # For some reason, we have to inline LazyObject.__init__ here to avoid
- # recursion
- self._wrapped = None
-
- def _setup(self):
- self._wrapped = self._setupfunc()
+from django.utils.functional import lazy, memoize, SimpleLazyObject
def auth(request):
"""
@@ -55,7 +35,7 @@ def auth(request):
return AnonymousUser()
return {
- 'user': ContextLazyObject(get_user),
+ 'user': SimpleLazyObject(get_user),
'messages': lazy(memoize(lambda: get_user().get_and_delete_messages(), {}, 0), list)(),
'perms': lazy(lambda: PermWrapper(get_user()), PermWrapper)(),
}
diff --git a/django/utils/functional.py b/django/utils/functional.py
index 23614d1712..4fbc8cd630 100644
--- a/django/utils/functional.py
+++ b/django/utils/functional.py
@@ -257,9 +257,8 @@ class LazyObject(object):
A wrapper for another class that can be used to delay instantiation of the
wrapped class.
- This is useful, for example, if the wrapped class needs to use Django
- settings at creation time: we want to permit it to be imported without
- accessing settings.
+ By subclassing, you have the opportunity to intercept and alter the
+ instantiation. If you don't need to do that, use SimpleLazyObject.
"""
def __init__(self):
self._wrapped = None
@@ -287,3 +286,26 @@ class LazyObject(object):
"""
raise NotImplementedError
+
+class SimpleLazyObject(LazyObject):
+ """
+ A lazy object initialised from any function.
+
+ Designed for compound objects of unknown type. For builtins or objects of
+ known type, use django.utils.functional.lazy.
+ """
+ def __init__(self, func):
+ """
+ Pass in a callable that returns the object to be wrapped.
+ """
+ self.__dict__['_setupfunc'] = func
+ # For some reason, we have to inline LazyObject.__init__ here to avoid
+ # recursion
+ self._wrapped = None
+
+ def __str__(self):
+ if self._wrapped is None: self._setup()
+ return str(self._wrapped)
+
+ def _setup(self):
+ self._wrapped = self._setupfunc()