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

arigo at codespeak.net arigo at codespeak.net
Sun Nov 7 16:07:09 CET 2010


Author: arigo
Date: Sun Nov  7 16:07:07 2010
New Revision: 78817

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.difference(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 16:07:07 2010
@@ -6,11 +6,9 @@
 
 frozenset_copy                  = SMM('copy', 1,
                                       doc='Return a shallow copy of a set.')
-frozenset_difference            = SMM('difference', 2,
-                                      doc='Return the difference of two sets'
-                                          ' as a new set.\n\n(i.e. all'
-                                          ' elements that are in this set but'
-                                          ' not the other.)')
+frozenset_difference            = SMM('difference', 1, varargs_w=True,
+                                      doc='Return a new set with elements in'
+                                          ' the set that are not in the others.')
 frozenset_intersection          = SMM('intersection', 1, varargs_w=True,
                                       doc='Return a new set with elements common'
                                           ' to the set and all others.')

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 16:07:07 2010
@@ -233,26 +233,28 @@
 def set_clear__Set(space, w_left):
     w_left.setdata.clear()
 
-def set_difference__Set_Set(space, w_left, w_other):
-    # optimization only (the general case works too)
+def sub__Set_Set(space, w_left, w_other):
     ld, rd = w_left.setdata, w_other.setdata
     new_ld = _difference_dict(space, ld, rd)
     return w_left._newobj(space, new_ld)
 
-set_difference__Set_Frozenset = set_difference__Set_Set
-frozenset_difference__Frozenset_Set = set_difference__Set_Set
-frozenset_difference__Frozenset_Frozenset = set_difference__Set_Set
-sub__Set_Set = set_difference__Set_Set
-sub__Set_Frozenset = set_difference__Set_Set
-sub__Frozenset_Set = set_difference__Set_Set
-sub__Frozenset_Frozenset = set_difference__Set_Set
+sub__Set_Frozenset = sub__Set_Set
+sub__Frozenset_Set = sub__Set_Set
+sub__Frozenset_Frozenset = sub__Set_Set
 
-def set_difference__Set_ANY(space, w_left, w_other):
-    ld, rd = w_left.setdata, make_setdata_from_w_iterable(space, w_other)
-    new_ld = _difference_dict(space, ld, rd)
-    return w_left._newobj(space, new_ld)
+def set_difference__Set(space, w_left, others_w):
+    result = w_left.setdata
+    if len(others_w) == 0:
+        result = result.copy()
+    for w_other in others_w:
+        if isinstance(w_other, W_BaseSetObject):
+            rd = w_other.setdata     # optimization only
+        else:
+            rd = make_setdata_from_w_iterable(space, w_other)
+        result = _difference_dict(space, result, rd)
+    return w_left._newobj(space, result)
 
-frozenset_difference__Frozenset_ANY = set_difference__Set_ANY
+frozenset_difference__Frozenset = set_difference__Set
 
 
 def set_difference_update__Set_Set(space, w_left, w_other):

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 16:07:07 2010
@@ -11,11 +11,9 @@
                                       doc='Remove all elements from this set.')
 set_copy                        = SMM('copy', 1,
                                       doc='Return a shallow copy of a set.')
-set_difference                  = SMM('difference', 2,
-                                      doc='Return the difference of two sets'
-                                          ' as a new set.\n\n(i.e. all'
-                                          ' elements that are in this set but'
-                                          ' not the other.)')
+set_difference                  = SMM('difference', 1, varargs_w=True,
+                                      doc='Return a new set with elements in'
+                                          ' the set that are not in the others.')
 set_difference_update           = SMM('difference_update', 2,
                                       doc='Remove all elements of another set'
                                           ' from this set.')

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 16:07:07 2010
@@ -300,3 +300,18 @@
         s = set([1,2,3])
         assert s.intersection() == s
         assert s.intersection() is not s
+
+    def test_difference(self):
+        assert set([1,2,3]).difference(set([2,3,4])) == set([1])
+        assert set([1,2,3]).difference(frozenset([2,3,4])) == set([1])
+        assert set([1,2,3]).difference([2,3,4]) == set([1])
+        assert set([1,2,3]).difference((2,3,4)) == set([1])
+        assert frozenset([1,2,3]).difference(set([2,3,4])) == frozenset([1])
+        assert frozenset([1,2,3]).difference(frozenset([2,3,4]))== frozenset([1])
+        assert frozenset([1,2,3]).difference([2,3,4]) == frozenset([1])
+        assert frozenset([1,2,3]).difference((2,3,4)) == frozenset([1])
+        assert set([1,2,3,4]).difference([4,5], set((0,1))) == set([2,3])
+        assert frozenset([1,2,3,4]).difference((4,5), [0,1]) == frozenset([2,3])
+        s = set([1,2,3])
+        assert s.difference() == s
+        assert s.difference() is not s



More information about the Pypy-commit mailing list