Commits

Antonio Cuni committed 25ca3d2 Draft

make sure that set operations on dict views work with sequences in general and when the dictview is the right operand. Also, reduce a bit the duplicated code with some metaprogramming

  • Participants
  • Parent commits 3cc63ec
  • Branches py3k

Comments (0)

Files changed (2)

File pypy/objspace/std/dictmultiobject.py

 repr__DictViewItems  = repr__DictViewKeys
 repr__DictViewValues = repr__DictViewKeys
 
-def and__DictViewKeys_DictViewKeys(space, w_dictview, w_otherview):
-    w_set = space.call_function(space.w_set, w_dictview)
-    space.call_method(w_set, "intersection_update", w_otherview)
-    return w_set
-and__DictViewKeys_settypedef = and__DictViewKeys_DictViewKeys
-and__DictViewItems_DictViewItems = and__DictViewKeys_DictViewKeys
-and__DictViewItems_settypedef = and__DictViewKeys_DictViewKeys
 
-def or__DictViewKeys_DictViewKeys(space, w_dictview, w_otherview):
-    w_set = space.call_function(space.w_set, w_dictview)
-    space.call_method(w_set, "update", w_otherview)
-    return w_set
-or__DictViewKeys_settypedef = or__DictViewKeys_DictViewKeys
-or__DictViewItems_DictViewItems = or__DictViewKeys_DictViewKeys
-or__DictViewItems_settypedef = or__DictViewKeys_DictViewKeys
+def generate_setops():
+    OPLIST = [
+        ('and', 'intersection_update'),
+        ('or', 'update'),
+        ('xor', 'symmetric_difference_update'),
+        ('sub', 'difference_update'),
+        ]
 
-def xor__DictViewKeys_DictViewKeys(space, w_dictview, w_otherview):
-    w_set = space.call_function(space.w_set, w_dictview)
-    space.call_method(w_set, "symmetric_difference_update", w_otherview)
-    return w_set
-xor__DictViewKeys_settypedef = xor__DictViewKeys_DictViewKeys
-xor__DictViewItems_DictViewItems = xor__DictViewKeys_DictViewKeys
-xor__DictViewItems_settypedef = xor__DictViewKeys_DictViewKeys
+    for (opname, methodname) in OPLIST:
+        src = py.code.Source("""
+        def {opname}__DictViewKeys_ANY(space, w_dictview, w_other):
+            w_set = space.call_function(space.w_set, w_dictview)
+            space.call_method(w_set, '{methodname}', w_other)
+            return w_set
 
-def sub__DictViewKeys_DictViewKeys(space, w_dictview, w_otherview):
-    w_set = space.call_function(space.w_set, w_dictview)
-    space.call_method(w_set, "difference_update", w_otherview)
-    return w_set
-sub__DictViewKeys_settypedef = sub__DictViewKeys_DictViewKeys
-sub__DictViewItems_DictViewItems = sub__DictViewKeys_DictViewKeys
-sub__DictViewItems_settypedef = sub__DictViewKeys_DictViewKeys
+        def {opname}__ANY_DictViewKeys(space, w_other, w_dictview):
+            w_set = space.call_function(space.w_set, w_dictview)
+            space.call_method(w_set, '{methodname}', w_other)
+            return w_set
+
+        {opname}__DictViewItems_ANY = {opname}__DictViewKeys_ANY
+        {opname}__ANY_DictViewItems = {opname}__ANY_DictViewKeys
+        """.format(opname=opname, methodname=methodname))
+        exec src.compile() in globals()
+
+generate_setops()
+
 
 # ____________________________________________________________
 

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

         assert d1.items() - d2.items() == set([('a', 1)])
         assert d1.items() - d3.items() == set([('a', 1), ('b', 2)])
 
-
+    def test_keys_items_set_operations_any_type(self):
+        d = {1: 'a', 2: 'b', 3: 'c'}
+        assert d.keys() & {1} == {1}
+        assert d.keys() & {1: 'foo'} == {1}
+        assert d.keys() & [1, 2] == {1, 2}
+        #
+        assert {1} & d.keys() == {1}
+        assert {1: 'foo'} & d.keys() == {1}
+        assert [1, 2] & d.keys() == {1, 2}
+        
 class AppTestStrategies(object):
     def setup_class(cls):
         if option.runappdirect: