[pypy-commit] pypy list-strategies: If list is empty after deletion (item, slice) switch to EmptyListStrategy

l.diekmann noreply at buildbot.pypy.org
Fri Sep 23 13:11:38 CEST 2011


Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: list-strategies
Changeset: r47436:3978ca76f7a3
Date: 2011-02-23 17:40 +0100
http://bitbucket.org/pypy/pypy/changeset/3978ca76f7a3/

Log:	If list is empty after deletion (item, slice) switch to
	EmptyListStrategy

diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -335,13 +335,16 @@
                 # Make a shallow copy to more easily handle the reversal case
                 sequence_w = list(sequence_w)
         for i in range(len2):
-            items[start] = sequence_w[i]
+            items[start] = self.unwrap(sequence_w[i])
             start += step
 
 
     def deleteitem(self, w_list, index):
         list_w = self.cast_from_void_star(w_list.storage)
         del list_w[index]
+        if len(list_w) == 0:
+            w_list.strategy = EmptyListStrategy()
+            w_list.strategy.init_from_list_w(w_list, list_w)
 
     def deleteslice(self, w_list, start, step, slicelength):
         items = self.cast_from_void_star(w_list.storage)
@@ -375,6 +378,10 @@
             assert start >= 0 # annotator hint
             del items[start:]
 
+        if len(items) == 0:
+            w_list.strategy = EmptyListStrategy()
+            w_list.strategy.init_from_list_w(w_list, items)
+
     def inplace_mul(self, w_list, times):
         list_w = self.cast_from_void_star(w_list.storage)
         list_w *= times
diff --git a/pypy/objspace/std/test/test_liststrategies.py b/pypy/objspace/std/test/test_liststrategies.py
--- a/pypy/objspace/std/test/test_liststrategies.py
+++ b/pypy/objspace/std/test/test_liststrategies.py
@@ -92,6 +92,17 @@
         l.insert(0, self.space.wrap(2))
         assert isinstance(l.strategy, IntegerListStrategy)
 
+    def test_delete(self):
+        l = W_ListObject([self.space.wrap(3)])
+        assert isinstance(l.strategy, IntegerListStrategy)
+        l.deleteitem(0)
+        assert isinstance(l.strategy, EmptyListStrategy)
+
+        l = W_ListObject([self.space.wrap(1), self.space.wrap(2)])
+        assert isinstance(l.strategy, IntegerListStrategy)
+        l.deleteslice(0, 1, 2)
+        assert isinstance(l.strategy, EmptyListStrategy)
+
     def test_setslice(self):
         l = W_ListObject([])
         assert isinstance(l.strategy, EmptyListStrategy)
@@ -103,6 +114,11 @@
         l.setslice(0, 1, 2, [self.space.wrap(4), self.space.wrap(5), self.space.wrap(6)])
         assert isinstance(l.strategy, IntegerListStrategy)
 
+        l = W_ListObject([self.space.wrap(1), self.space.wrap('b'), self.space.wrap(3)])
+        assert isinstance(l.strategy, ObjectListStrategy)
+        l.setslice(0, 1, 2, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
+        assert isinstance(l.strategy, ObjectListStrategy)
+
         l = W_ListObject([self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])
         assert isinstance(l.strategy, IntegerListStrategy)
         l.setslice(0, 1, 2, [self.space.wrap('a'), self.space.wrap('b'), self.space.wrap('c')])


More information about the pypy-commit mailing list