Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1479 from nickbruun/ticket_20924

Proxy __len__ and __contains__ for LazyObject
  • Loading branch information...
commit aa01c99f5587b36c354e80c682ad52e1a3b41455 2 parents c2907a6 + 7a698c0
Alex Gaynor authored August 18, 2013
3  django/utils/functional.py
@@ -270,6 +270,9 @@ def __setitem__(self, key, value):
270 270
     def __delitem__(self, key):
271 271
         del self[key]
272 272
 
  273
+    __len__ = new_method_proxy(len)
  274
+    __contains__ = new_method_proxy(operator.contains)
  275
+
273 276
 
274 277
 # Workaround for http://bugs.python.org/issue12370
275 278
 _super = super
13  tests/utils_tests/test_simplelazyobject.py
@@ -136,6 +136,9 @@ def test_dict(self):
136 136
         self.assertEqual(lazydict['one'], 1)
137 137
         lazydict['one'] = -1
138 138
         self.assertEqual(lazydict['one'], -1)
  139
+        self.assertTrue('one' in lazydict)
  140
+        self.assertFalse('two' in lazydict)
  141
+        self.assertEqual(len(lazydict), 1)
139 142
         del lazydict['one']
140 143
         with self.assertRaises(KeyError):
141 144
             lazydict['one']
@@ -183,3 +186,13 @@ def test_pickle_py2_regression(self):
183 186
 
184 187
             # This would fail with "TypeError: expected string or Unicode object, NoneType found".
185 188
             pickled = cPickle.dumps(x)
  189
+
  190
+    def test_list_set(self):
  191
+        lazy_list = SimpleLazyObject(lambda: [1, 2, 3, 4, 5])
  192
+        lazy_set = SimpleLazyObject(lambda: set([1, 2, 3, 4]))
  193
+        self.assertTrue(1 in lazy_list)
  194
+        self.assertTrue(1 in lazy_set)
  195
+        self.assertFalse(6 in lazy_list)
  196
+        self.assertFalse(6 in lazy_set)
  197
+        self.assertEqual(len(lazy_list), 5)
  198
+        self.assertEqual(len(lazy_set), 4)

1 note on commit aa01c99

Aymeric Augustin
Owner

That commit created a regression that's blocking the 1.7 release: https://code.djangoproject.com/ticket/21840

@nickbruun @alex Do you have an idea to avoid that problem? Or should I simply revert the commit?

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