[pypy-commit] pypy py3k: implement containment operators for dict views

antocuni noreply at buildbot.pypy.org
Tue Oct 2 16:01:37 CEST 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r57718:f35922ab0bb5
Date: 2012-10-02 16:01 +0200
http://bitbucket.org/pypy/pypy/changeset/f35922ab0bb5/

Log:	implement containment operators for dict views

diff --git a/pypy/objspace/std/dictmultiobject.py b/pypy/objspace/std/dictmultiobject.py
--- a/pypy/objspace/std/dictmultiobject.py
+++ b/pypy/objspace/std/dictmultiobject.py
@@ -1031,6 +1031,24 @@
         """.format(opname=opname, methodname=methodname))
         exec src.compile() in globals()
 
+
+    for opname in ['lt', 'le', 'eq', 'ne', 'ge', 'gt']:
+        src = py.code.Source("""
+        def {opname}__DictViewKeys_ANY(space, w_dictview, w_other):
+            w_left = space.call_function(space.w_set, w_dictview)
+            w_right = space.call_function(space.w_set, w_other)
+            return space.{opname}(w_left, w_right)
+
+        def {opname}__ANY_DictViewKeys(space, w_other, w_dictview):
+            w_left = space.call_function(space.w_set, w_other)
+            w_right = space.call_function(space.w_set, w_dictview)
+            return space.{opname}(w_left, w_right)
+
+        {opname}__DictViewItems_ANY = {opname}__DictViewKeys_ANY
+        {opname}__ANY_DictViewItems = {opname}__ANY_DictViewKeys
+        """.format(opname=opname))
+        exec src.compile() in globals()
+
 generate_setops()
 
 
diff --git a/pypy/objspace/std/test/test_dictmultiobject.py b/pypy/objspace/std/test/test_dictmultiobject.py
--- a/pypy/objspace/std/test/test_dictmultiobject.py
+++ b/pypy/objspace/std/test/test_dictmultiobject.py
@@ -796,7 +796,53 @@
         #
         assert d.keys() - {1} == {2, 3}
         assert {1, 4} - d.keys() == {4}
-        
+
+    def test_keys_items_contained(self):
+        def helper(fn):
+            empty = fn(dict())
+            empty2 = fn(dict())
+            smaller = fn({1:1, 2:2})
+            larger = fn({1:1, 2:2, 3:3})
+            larger2 = fn({1:1, 2:2, 3:3})
+            larger3 = fn({4:1, 2:2, 3:3})
+
+            assert smaller <  larger
+            assert smaller <= larger
+            assert larger >  smaller
+            assert larger >= smaller
+
+            assert not smaller >= larger
+            assert not smaller >  larger
+            assert not larger  <= smaller
+            assert not larger  <  smaller
+
+            assert not smaller <  larger3
+            assert not smaller <= larger3
+            assert not larger3 >  smaller
+            assert not larger3 >= smaller
+
+            # Inequality strictness
+            assert larger2 >= larger
+            assert larger2 <= larger
+            assert not larger2 > larger
+            assert not larger2 < larger
+
+            assert larger == larger2
+            assert smaller != larger
+
+            # There is an optimization on the zero-element case.
+            assert empty == empty2
+            assert not empty != empty2
+            assert not empty == smaller
+            assert empty != smaller
+
+            # With the same size, an elementwise compare happens
+            assert larger != larger3
+            assert not larger == larger3
+
+        helper(lambda x: x.keys())
+        helper(lambda x: x.items())
+
 class AppTestStrategies(object):
     def setup_class(cls):
         if option.runappdirect:


More information about the pypy-commit mailing list