[pypy-commit] pypy set-strategies: switch back to empty strategy on remove and clear

l.diekmann noreply at buildbot.pypy.org
Thu Nov 10 13:49:47 CET 2011


Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: set-strategies
Changeset: r49162:715728d2fe02
Date: 2011-05-17 14:14 +0200
http://bitbucket.org/pypy/pypy/changeset/715728d2fe02/

Log:	switch back to empty strategy on remove and clear

diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py
--- a/pypy/objspace/std/setobject.py
+++ b/pypy/objspace/std/setobject.py
@@ -97,6 +97,10 @@
         self.strategy = space.fromcache(ObjectSetStrategy)
         self.sstorage = self.strategy.cast_to_void_star(d)
 
+    def switch_to_empty_strategy(self):
+        self.strategy = self.space.fromcache(EmptySetStrategy)
+        self.sstorage = self.strategy.get_empty_storage()
+
     # _____________ strategy methods ________________
 
     def clear(self):
@@ -305,7 +309,7 @@
         return len(self.cast_from_void_star(w_set.sstorage))
 
     def clear(self, w_set):
-        self.cast_from_void_star(w_set.sstorage).clear()
+        w_set.switch_to_empty_strategy()
 
     def copy(self, w_set):
         #XXX do not copy FrozenDict
@@ -937,6 +941,8 @@
     Returns True if successfully removed.
     """
     x = w_left.discard(w_item)
+    if w_left.length() == 0:
+        w_left.switch_to_empty_strategy()
     return x
 
 def set_discard__Set_ANY(space, w_left, w_item):
diff --git a/pypy/objspace/std/test/test_setstrategies.py b/pypy/objspace/std/test/test_setstrategies.py
--- a/pypy/objspace/std/test/test_setstrategies.py
+++ b/pypy/objspace/std/test/test_setstrategies.py
@@ -26,12 +26,13 @@
 
         s1 = W_SetObject(self.space, self.wrapped([1,2,3,4,5]))
         s2 = W_SetObject(self.space, self.wrapped(["six", "seven"]))
-        s1.symmetric_difference_update(s2)
+        s1.update(s2)
         assert s1.strategy is self.space.fromcache(ObjectSetStrategy)
 
+    def test_symmetric_difference(self):
         s1 = W_SetObject(self.space, self.wrapped([1,2,3,4,5]))
         s2 = W_SetObject(self.space, self.wrapped(["six", "seven"]))
-        s1.update(s2)
+        s1.symmetric_difference_update(s2)
         assert s1.strategy is self.space.fromcache(ObjectSetStrategy)
 
     def test_intersection(self):
@@ -40,3 +41,14 @@
         s3 = s1.intersect(s2)
         assert s3.strategy is self.space.fromcache(IntegerSetStrategy)
 
+    def test_clear(self):
+        s1 = W_SetObject(self.space, self.wrapped([1,2,3,4,5]))
+        s1.clear()
+        assert s1.strategy is self.space.fromcache(EmptySetStrategy)
+
+    def test_remove(self):
+        from pypy.objspace.std.setobject import set_remove__Set_ANY
+        s1 = W_SetObject(self.space, self.wrapped([1]))
+        set_remove__Set_ANY(self.space, s1, self.space.wrap(1))
+        assert s1.strategy is self.space.fromcache(EmptySetStrategy)
+


More information about the pypy-commit mailing list