From bcef28349a52ff16c9c022b2722138ee9fe62a81 Mon Sep 17 00:00:00 2001 From: Luke Plant Date: Tue, 5 Jan 2010 12:06:45 +0000 Subject: [PATCH] Fixed #12506 - 'lazy' fails when there are multiple expected classes with the same method Thanks to Alex for report and patch git-svn-id: http://code.djangoproject.com/svn/django/trunk@12104 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/functional.py | 6 +++++- tests/regressiontests/utils/functional.py | 10 ++++++++++ tests/regressiontests/utils/tests.py | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/regressiontests/utils/functional.py diff --git a/django/utils/functional.py b/django/utils/functional.py index 43b7ab1437..e52ab76a38 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -167,9 +167,13 @@ def lazy(func, *resultclasses): for resultclass in resultclasses: cls.__dispatch[resultclass] = {} for (k, v) in resultclass.__dict__.items(): + # All __promise__ return the same wrapper method, but they + # also do setup, inserting the method into the dispatch + # dict. + meth = cls.__promise__(resultclass, k, v) if hasattr(cls, k): continue - setattr(cls, k, cls.__promise__(resultclass, k, v)) + setattr(cls, k, meth) cls._delegate_str = str 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." diff --git a/tests/regressiontests/utils/functional.py b/tests/regressiontests/utils/functional.py new file mode 100644 index 0000000000..72610154d8 --- /dev/null +++ b/tests/regressiontests/utils/functional.py @@ -0,0 +1,10 @@ +from unittest import TestCase + +from django.utils.functional import lazy + + +class FunctionalTestCase(TestCase): + def test_lazy(self): + t = lazy(lambda: tuple(range(3)), list, tuple) + for a, b in zip(t(), range(3)): + self.assertEqual(a, b) diff --git a/tests/regressiontests/utils/tests.py b/tests/regressiontests/utils/tests.py index fe5f463110..cd93fb9c01 100644 --- a/tests/regressiontests/utils/tests.py +++ b/tests/regressiontests/utils/tests.py @@ -12,6 +12,7 @@ import datastructures import itercompat from decorators import DecoratorFromMiddlewareTests +from functional import FunctionalTestCase # We need this because "datastructures" uses sorted() and the tests are run in # the scope of this module.