Fixed #15811 - lazy() doesn't take into account methods defined in parents

Thanks to abki for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16157 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2011-05-05 00:09:51 +00:00
parent 127f1e4190
commit 23b32c7554
2 changed files with 22 additions and 8 deletions

View File

@ -68,14 +68,15 @@ def lazy(func, *resultclasses):
cls.__dispatch = {} cls.__dispatch = {}
for resultclass in resultclasses: for resultclass in resultclasses:
cls.__dispatch[resultclass] = {} cls.__dispatch[resultclass] = {}
for (k, v) in resultclass.__dict__.items(): for type_ in reversed(resultclass.mro()):
# All __promise__ return the same wrapper method, but they for (k, v) in type_.__dict__.items():
# also do setup, inserting the method into the dispatch # All __promise__ return the same wrapper method, but they
# dict. # also do setup, inserting the method into the dispatch
meth = cls.__promise__(resultclass, k, v) # dict.
if hasattr(cls, k): meth = cls.__promise__(resultclass, k, v)
continue if hasattr(cls, k):
setattr(cls, k, meth) continue
setattr(cls, k, meth)
cls._delegate_str = str in resultclasses cls._delegate_str = str in resultclasses
cls._delegate_unicode = unicode in resultclasses cls._delegate_unicode = unicode in resultclasses
assert not (cls._delegate_str and cls._delegate_unicode), "Cannot call lazy() with both str and unicode return types." assert not (cls._delegate_str and cls._delegate_unicode), "Cannot call lazy() with both str and unicode return types."

View File

@ -7,3 +7,16 @@ class FunctionalTestCase(unittest.TestCase):
t = lazy(lambda: tuple(range(3)), list, tuple) t = lazy(lambda: tuple(range(3)), list, tuple)
for a, b in zip(t(), range(3)): for a, b in zip(t(), range(3)):
self.assertEqual(a, b) self.assertEqual(a, b)
def test_lazy_base_class(self):
"""Test that lazy also finds base class methods in the proxy object"""
class Base(object):
def base_method(self):
pass
class Klazz(Base):
pass
t = lazy(lambda: Klazz(), Klazz)()
self.assertTrue('base_method' in dir(t))