mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes. Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702 git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -18,7 +18,7 @@ def memoize(func, cache):
|
||||
return result
|
||||
return wrapper
|
||||
|
||||
class Promise:
|
||||
class Promise(object):
|
||||
"""
|
||||
This is just a base class for the proxy class created in
|
||||
the closure of the lazy function. It can be used to recognize
|
||||
@@ -47,6 +47,11 @@ def lazy(func, *resultclasses):
|
||||
self.__dispatch[resultclass] = {}
|
||||
for (k, v) in resultclass.__dict__.items():
|
||||
setattr(self, k, self.__promise__(resultclass, k, v))
|
||||
self._delegate_str = str in resultclasses
|
||||
self._delegate_unicode = unicode in resultclasses
|
||||
assert not (self._delegate_str and self._delegate_unicode), "Cannot call lazy() with both str and unicode return types."
|
||||
if self._delegate_unicode:
|
||||
self.__unicode__ = self.__unicode_cast
|
||||
|
||||
def __promise__(self, klass, funcname, func):
|
||||
# Builds a wrapper around some magic method and registers that magic
|
||||
@@ -62,8 +67,56 @@ def lazy(func, *resultclasses):
|
||||
self.__dispatch[klass][funcname] = func
|
||||
return __wrapper__
|
||||
|
||||
def __unicode_cast(self):
|
||||
return self.__func(*self.__args, **self.__kw)
|
||||
|
||||
def __str__(self):
|
||||
# As __str__ is always a method on the type (class), it is looked
|
||||
# up (and found) there first. So we can't just assign to it on a
|
||||
# per-instance basis in __init__.
|
||||
if self._delegate_str:
|
||||
return str(self.__func(*self.__args, **self.__kw))
|
||||
else:
|
||||
return Promise.__str__(self)
|
||||
|
||||
def __cmp__(self, rhs):
|
||||
if self._delegate_str:
|
||||
s = str(self.__func(*self.__args, **self.__kw))
|
||||
elif self._delegate_unicode:
|
||||
s = unicode(self.__func(*self.__args, **self.__kw))
|
||||
else:
|
||||
s = self.__func(*self.__args, **self.__kw)
|
||||
if isinstance(rhs, Promise):
|
||||
return -cmp(rhs, s)
|
||||
else:
|
||||
return cmp(s, rhs)
|
||||
|
||||
def __mod__(self, rhs):
|
||||
if self._delegate_str:
|
||||
return str(self) % rhs
|
||||
elif self._delegate_unicode:
|
||||
return unicode(self) % rhs
|
||||
else:
|
||||
raise AssertionError('__mod__ not supported for non-string types')
|
||||
|
||||
def __wrapper__(*args, **kw):
|
||||
# Creates the proxy object, instead of the actual value.
|
||||
return __proxy__(args, kw)
|
||||
|
||||
return __wrapper__
|
||||
|
||||
def allow_lazy(func, *resultclasses):
|
||||
"""
|
||||
A decorator that allows a function to be called with one or more lazy
|
||||
arguments. If none of the args are lazy, the function is evaluated
|
||||
immediately, otherwise a __proxy__ is returned that will evaluate the
|
||||
function when needed.
|
||||
"""
|
||||
def wrapper(*args, **kwargs):
|
||||
for arg in list(args) + kwargs.values():
|
||||
if isinstance(arg, Promise):
|
||||
break
|
||||
else:
|
||||
return func(*args, **kwargs)
|
||||
return lazy(func, *resultclasses)(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
Reference in New Issue
Block a user