Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5560 -- Improved the way we create __str__ and __unicode__ met…

…hods in

lazy() objects. This fixes things for Jython and makes the code more readable,
even for CPython. Thanks, Leo Soto.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6587 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bab4fdc9fd1be47575f440816b4dfc422947d368 1 parent 2adcb3f
Malcolm Tredinnick authored October 21, 2007

Showing 1 changed file with 7 additions and 9 deletions. Show diff stats Hide diff stats

  1. 16  django/utils/functional.py
16  django/utils/functional.py
@@ -53,7 +53,11 @@ def __init__(self, args, kw):
53 53
             self._delegate_unicode = unicode in resultclasses
54 54
             assert not (self._delegate_str and self._delegate_unicode), "Cannot call lazy() with both str and unicode return types."
55 55
             if self._delegate_unicode:
56  
-                self.__unicode__ = self.__unicode_cast
  56
+                # Each call to lazy() makes a new __proxy__ object, so this
  57
+                # doesn't interfere with any other lazy() results.
  58
+                __proxy__.__unicode__ = __proxy__.__unicode_cast
  59
+            elif self._delegate_str:
  60
+                __proxy__.__str__ = __proxy__.__str_cast
57 61
 
58 62
         def __promise__(self, klass, funcname, func):
59 63
             # Builds a wrapper around some magic method and registers that magic
@@ -72,14 +76,8 @@ def __wrapper__(*args, **kw):
72 76
         def __unicode_cast(self):
73 77
             return self.__func(*self.__args, **self.__kw)
74 78
 
75  
-        def __str__(self):
76  
-            # As __str__ is always a method on the type (class), it is looked
77  
-            # up (and found) there first. So we can't just assign to it on a
78  
-            # per-instance basis in __init__.
79  
-            if self._delegate_str:
80  
-                return str(self.__func(*self.__args, **self.__kw))
81  
-            else:
82  
-                return Promise.__str__(self)
  79
+        def __str_cast(self):
  80
+            return str(self.__func(*self.__args, **self.__kw))
83 81
 
84 82
         def __cmp__(self, rhs):
85 83
             if self._delegate_str:

0 notes on commit bab4fdc

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