Merge pull request #1479 from nickbruun/ticket_20924
Proxy __len__ and __contains__ for LazyObject
This commit is contained in:
commit
aa01c99f55
|
@ -270,6 +270,9 @@ class LazyObject(object):
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
del self[key]
|
del self[key]
|
||||||
|
|
||||||
|
__len__ = new_method_proxy(len)
|
||||||
|
__contains__ = new_method_proxy(operator.contains)
|
||||||
|
|
||||||
|
|
||||||
# Workaround for http://bugs.python.org/issue12370
|
# Workaround for http://bugs.python.org/issue12370
|
||||||
_super = super
|
_super = super
|
||||||
|
|
|
@ -136,6 +136,9 @@ class TestUtilsSimpleLazyObject(TestCase):
|
||||||
self.assertEqual(lazydict['one'], 1)
|
self.assertEqual(lazydict['one'], 1)
|
||||||
lazydict['one'] = -1
|
lazydict['one'] = -1
|
||||||
self.assertEqual(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']
|
del lazydict['one']
|
||||||
with self.assertRaises(KeyError):
|
with self.assertRaises(KeyError):
|
||||||
lazydict['one']
|
lazydict['one']
|
||||||
|
@ -183,3 +186,13 @@ class TestUtilsSimpleLazyObject(TestCase):
|
||||||
|
|
||||||
# This would fail with "TypeError: expected string or Unicode object, NoneType found".
|
# This would fail with "TypeError: expected string or Unicode object, NoneType found".
|
||||||
pickled = cPickle.dumps(x)
|
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)
|
||||||
|
|
Loading…
Reference in New Issue