Author: Antonio Cuni <[email protected]>
Branch: py3k
Changeset: r57716:25ca3d2037d4
Date: 2012-10-02 14:45 +0200
http://bitbucket.org/pypy/pypy/changeset/25ca3d2037d4/
Log: 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
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
@@ -1005,37 +1005,34 @@
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()
+
# ____________________________________________________________
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
@@ -784,7 +784,16 @@
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:
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit