[pypy-svn] r78815 - in pypy/branch/fast-forward/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Sun Nov 7 15:43:54 CET 2010


Author: arigo
Date: Sun Nov  7 15:43:53 2010
New Revision: 78815

Modified:
   pypy/branch/fast-forward/pypy/objspace/std/frozensettype.py
   pypy/branch/fast-forward/pypy/objspace/std/setobject.py
   pypy/branch/fast-forward/pypy/objspace/std/settype.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_setobject.py
Log:
set.union(more_than_one_argument).


Modified: pypy/branch/fast-forward/pypy/objspace/std/frozensettype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/frozensettype.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/frozensettype.py	Sun Nov  7 15:43:53 2010
@@ -26,10 +26,9 @@
                                           ' two sets as a new set.\n\n(i.e.'
                                           ' all elements that are in exactly'
                                           ' one of the sets.)')
-frozenset_union                 = SMM('union', 2,
-                                      doc='Return the union of two sets as a'
-                                          ' new set.\n\n(i.e. all elements'
-                                          ' that are in either set.)')
+frozenset_union                 = SMM('union', 1, varargs_w=True,
+                                      doc='Return a new set with elements'
+                                          ' from the set and all others.')
 frozenset_reduce                = SMM('__reduce__',1,
                                       doc='Return state information for'
                                           ' pickling.')

Modified: pypy/branch/fast-forward/pypy/objspace/std/setobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/setobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/setobject.py	Sun Nov  7 15:43:53 2010
@@ -582,30 +582,28 @@
 
 inplace_xor__Set_Frozenset = inplace_xor__Set_Set
 
-def set_union__Set_Set(space, w_left, w_other):
-    # optimization only (the general case works too)
+def or__Set_Set(space, w_left, w_other):
     ld, rd = w_left.setdata, w_other.setdata
     result = ld.copy()
     result.update(rd)
     return w_left._newobj(space, result)
 
-set_union__Set_Frozenset = set_union__Set_Set
-set_union__Frozenset_Set = set_union__Set_Set
-set_union__Frozenset_Frozenset = set_union__Set_Set
-or__Set_Set = set_union__Set_Set
-or__Set_Frozenset = set_union__Set_Set
-or__Frozenset_Set = set_union__Set_Set
-or__Frozenset_Frozenset = set_union__Set_Set
+or__Set_Frozenset = or__Set_Set
+or__Frozenset_Set = or__Set_Set
+or__Frozenset_Frozenset = or__Set_Set
 
-
-def set_union__Set_ANY(space, w_left, w_other):
+def set_union__Set(space, w_left, others_w):
     ld = w_left.setdata
     result = ld.copy()
-    for w_key in space.listview(w_other):
-        result[w_key] = None
+    for w_other in others_w:
+        if isinstance(w_other, W_BaseSetObject):
+            result.update(w_other.setdata)     # optimization only
+        else:
+            for w_key in space.listview(w_other):
+                result[w_key] = None
     return w_left._newobj(space, result)
 
-frozenset_union__Frozenset_ANY = set_union__Set_ANY
+frozenset_union__Frozenset = set_union__Set
 
 def len__Set(space, w_left):
     return space.newint(len(w_left.setdata))

Modified: pypy/branch/fast-forward/pypy/objspace/std/settype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/settype.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/settype.py	Sun Nov  7 15:43:53 2010
@@ -52,10 +52,9 @@
 set_symmetric_difference_update = SMM('symmetric_difference_update', 2,
                                       doc='Update a set with the symmetric'
                                           ' difference of itself and another.')
-set_union                       = SMM('union', 2,
-                                      doc='Return the union of two sets as a'
-                                          ' new set.\n\n(i.e. all elements'
-                                          ' that are in either set.)')
+set_union                       = SMM('union', 1, varargs_w=True,
+                                      doc='Return a new set with elements'
+                                          ' from the set and all others.')
 set_update                      = SMM('update', 2,
                                       doc='Update a set with the union of'
                                           ' itself and another.')

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_setobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_setobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_setobject.py	Sun Nov  7 15:43:53 2010
@@ -61,6 +61,10 @@
         a = set([4, 5])
         b = a.union([5, 7])
         assert sorted(b) == [4, 5, 7]
+        c = a.union([5, 7], [1], set([9,7]), frozenset([2]), frozenset())
+        assert sorted(c) == [1, 2, 4, 5, 7, 9]
+        d = a.union()
+        assert d == a
 
     def test_compare(self):
         raises(TypeError, cmp, set('abc'), set('abd'))



More information about the Pypy-commit mailing list