1. Jeremy Thurgood
  2. pypy

Commits

Antonio Cuni  committed f35922a Draft

implement containment operators for dict views

  • Participants
  • Parent commits 3110468
  • Branches py3k

Comments (0)

Files changed (2)

File pypy/objspace/std/dictmultiobject.py

View file
         """.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()
 
 

File pypy/objspace/std/test/test_dictmultiobject.py

View file
         #
         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: