Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6217 -- Reduced the memory used by each ugettext_lazy() call.

Thanks to Jakub Wilk for analysis and the initial patch.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8120 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6a287ed946e117d9daf757b9aff52087c93ad2ac 1 parent 7d1c147
Malcolm Tredinnick authored July 27, 2008
1  AUTHORS
@@ -407,6 +407,7 @@ answer newbie questions, and generally made Django that much better:
407 407
     charly.wilhelm@gmail.com
408 408
     Rachel Willmer <http://www.willmer.com/kb/>
409 409
     Gary Wilson <gary.wilson@gmail.com>
  410
+    Jakub Wilk <ubanus@users.sf.net>
410 411
     Jakub Wiśniowski <restless.being@gmail.com>
411 412
     Maciej Wiśniowski <pigletto@gmail.com>
412 413
     wojtek
3  django/db/models/fields/__init__.py
@@ -191,7 +191,8 @@ def validate(self, field_data, all_data):
191 191
     def set_attributes_from_name(self, name):
192 192
         self.name = name
193 193
         self.attname, self.column = self.get_attname_column()
194  
-        self.verbose_name = self.verbose_name or (name and name.replace('_', ' '))
  194
+        if self.verbose_name is None and name:
  195
+            self.verbose_name = name.replace('_', ' ')
195 196
 
196 197
     def contribute_to_class(self, cls, name):
197 198
         self.set_attributes_from_name(name)
53  django/utils/functional.py
@@ -148,42 +148,51 @@ def lazy(func, *resultclasses):
148 148
     function is evaluated on every access.
149 149
     """
150 150
     class __proxy__(Promise):
151  
-        # This inner class encapsulates the code that should be evaluated
152  
-        # lazily. On calling of one of the magic methods it will force
153  
-        # the evaluation and store the result. Afterwards, the result
154  
-        # is delivered directly. So the result is memoized.
  151
+        """
  152
+        Encapsulate a function call and act as a proxy for methods that are
  153
+        called on the result of that function. The function is not evaluated
  154
+        until one of the methods on the result is called.
  155
+        """
  156
+        __dispatch = None
  157
+
155 158
         def __init__(self, args, kw):
156 159
             self.__func = func
157 160
             self.__args = args
158 161
             self.__kw = kw
159  
-            self.__dispatch = {}
  162
+            if self.__dispatch is None:
  163
+                self.__prepare_class__()
  164
+
  165
+        def __prepare_class__(cls):
  166
+            cls.__dispatch = {}
160 167
             for resultclass in resultclasses:
161  
-                self.__dispatch[resultclass] = {}
  168
+                cls.__dispatch[resultclass] = {}
162 169
                 for (k, v) in resultclass.__dict__.items():
163  
-                    setattr(self, k, self.__promise__(resultclass, k, v))
164  
-            self._delegate_str = str in resultclasses
165  
-            self._delegate_unicode = unicode in resultclasses
166  
-            assert not (self._delegate_str and self._delegate_unicode), "Cannot call lazy() with both str and unicode return types."
167  
-            if self._delegate_unicode:
168  
-                # Each call to lazy() makes a new __proxy__ object, so this
169  
-                # doesn't interfere with any other lazy() results.
170  
-                __proxy__.__unicode__ = __proxy__.__unicode_cast
171  
-            elif self._delegate_str:
172  
-                __proxy__.__str__ = __proxy__.__str_cast
173  
-
174  
-        def __promise__(self, klass, funcname, func):
  170
+                    if hasattr(cls, k):
  171
+                        continue
  172
+                    setattr(cls, k, cls.__promise__(resultclass, k, v))
  173
+            cls._delegate_str = str in resultclasses
  174
+            cls._delegate_unicode = unicode in resultclasses
  175
+            assert not (cls._delegate_str and cls._delegate_unicode), "Cannot call lazy() with both str and unicode return types."
  176
+            if cls._delegate_unicode:
  177
+                cls.__unicode__ = cls.__unicode_cast
  178
+            elif cls._delegate_str:
  179
+                cls.__str__ = cls.__str_cast
  180
+        __prepare_class__ = classmethod(__prepare_class__)
  181
+
  182
+        def __promise__(cls, klass, funcname, func):
175 183
             # Builds a wrapper around some magic method and registers that magic
176 184
             # method for the given type and method name.
177  
-            def __wrapper__(*args, **kw):
  185
+            def __wrapper__(self, *args, **kw):
178 186
                 # Automatically triggers the evaluation of a lazy value and
179 187
                 # applies the given magic method of the result type.
180 188
                 res = self.__func(*self.__args, **self.__kw)
181 189
                 return self.__dispatch[type(res)][funcname](res, *args, **kw)
182 190
 
183  
-            if klass not in self.__dispatch:
184  
-                self.__dispatch[klass] = {}
185  
-            self.__dispatch[klass][funcname] = func
  191
+            if klass not in cls.__dispatch:
  192
+                cls.__dispatch[klass] = {}
  193
+            cls.__dispatch[klass][funcname] = func
186 194
             return __wrapper__
  195
+        __promise__ = classmethod(__promise__)
187 196
 
188 197
         def __unicode_cast(self):
189 198
             return self.__func(*self.__args, **self.__kw)

0 notes on commit 6a287ed

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