From b9ef96e73cf2f2613eb6ca96038ef8c4271ac116 Mon Sep 17 00:00:00 2001 From: Nick Bruun Date: Thu, 15 Aug 2013 20:59:58 +0200 Subject: [PATCH 1/2] Regression test and patch for ticket #20924. --- django/utils/functional.py | 8 ++++++++ tests/utils_tests/test_simplelazyobject.py | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/django/utils/functional.py b/django/utils/functional.py index d5d52205d7..465de283e5 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -270,6 +270,14 @@ class LazyObject(object): def __delitem__(self, key): del self[key] + @new_method_proxy + def __len__(self): + return len(self) + + @new_method_proxy + def __contains__(self, key): + return key in self + # Workaround for http://bugs.python.org/issue12370 _super = super diff --git a/tests/utils_tests/test_simplelazyobject.py b/tests/utils_tests/test_simplelazyobject.py index 7b681d9290..f16b5e4da1 100644 --- a/tests/utils_tests/test_simplelazyobject.py +++ b/tests/utils_tests/test_simplelazyobject.py @@ -136,6 +136,9 @@ class TestUtilsSimpleLazyObject(TestCase): self.assertEqual(lazydict['one'], 1) lazydict['one'] = -1 self.assertEqual(lazydict['one'], -1) + self.assertTrue('one' in lazydict) + self.assertFalse('two' in lazydict) + self.assertEqual(len(lazydict), 1) del lazydict['one'] with self.assertRaises(KeyError): lazydict['one'] @@ -183,3 +186,13 @@ class TestUtilsSimpleLazyObject(TestCase): # This would fail with "TypeError: expected string or Unicode object, NoneType found". pickled = cPickle.dumps(x) + + def test_list_set(self): + lazy_list = SimpleLazyObject(lambda: [1, 2, 3, 4, 5]) + lazy_set = SimpleLazyObject(lambda: set([1, 2, 3, 4])) + self.assertTrue(1 in lazy_list) + self.assertTrue(1 in lazy_set) + self.assertFalse(6 in lazy_list) + self.assertFalse(6 in lazy_set) + self.assertEqual(len(lazy_list), 5) + self.assertEqual(len(lazy_set), 4) From 7a698c05b56d7d5e267241dc71c16c403efd2965 Mon Sep 17 00:00:00 2001 From: Nick Bruun Date: Sun, 18 Aug 2013 15:58:55 +0200 Subject: [PATCH 2/2] Update LazyObject method proxy declarations to simpler form. --- django/utils/functional.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/django/utils/functional.py b/django/utils/functional.py index 465de283e5..7336572b35 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -270,13 +270,8 @@ class LazyObject(object): def __delitem__(self, key): del self[key] - @new_method_proxy - def __len__(self): - return len(self) - - @new_method_proxy - def __contains__(self, key): - return key in self + __len__ = new_method_proxy(len) + __contains__ = new_method_proxy(operator.contains) # Workaround for http://bugs.python.org/issue12370