[pypy-commit] pypy list-strategies: Switch RangeListStrategy to EmptyListStrategy if length is zero

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


Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: list-strategies
Changeset: r47456:025d68e4eb4c
Date: 2011-03-09 11:38 +0100
http://bitbucket.org/pypy/pypy/changeset/025d68e4eb4c/

Log:	Switch RangeListStrategy to EmptyListStrategy if length is zero

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
@@ -23,8 +23,12 @@
     return wrapper._content
 
 def make_range_list(space, start, step, length):
-    storage = cast_to_void_star((start, step, length), "integer")
-    strategy = RangeListStrategy(space)
+    if length <= 0:
+        storage = cast_to_void_star(None)
+        strategy = EmptyListStrategy(space)
+    else:
+        storage = cast_to_void_star((start, step, length), "integer")
+        strategy = RangeListStrategy(space)
     return W_ListObject.from_storage_and_strategy(space, storage, strategy)
 
 # don't know where to put this function, so it is global for now
@@ -339,17 +343,20 @@
         if index < 0:
             index += self.length(w_list)
 
+        #XXX merge these parts
         l = self.cast_from_void_star(w_list.storage)
         if index == 0:
-            r = self.getitem(w_list, 0)
+            r = self.getitem(w_list, index)
             new = cast_to_void_star((l[0]+l[1],l[1],l[2]-1), "integer")
             w_list.storage = new
+            w_list.check_empty_strategy()
             return r
 
         if index == self.length(w_list)-1:
-            r = self.getitem(w_list, -1)
+            r = self.getitem(w_list, index)
             new = cast_to_void_star((l[0],l[1],l[2]-1), "integer")
             w_list.storage = new
+            w_list.check_empty_strategy()
             return r
 
         self.switch_to_integer_strategy(w_list)
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
@@ -179,3 +179,15 @@
         assert isinstance(l.strategy, RangeListStrategy)
         l.append(self.space.wrap(16))
         assert isinstance(l.strategy, RangeListStrategy)
+
+    def test_empty_range(self):
+        l = make_range_list(self.space, 0, 0, 0)
+        assert isinstance(l.strategy, EmptyListStrategy)
+
+        l = make_range_list(self.space, 1, 1, 10)
+        print l.getitems()
+        for i in l.getitems():
+            assert isinstance(l.strategy, RangeListStrategy)
+            l.pop(-1)
+
+        assert isinstance(l.strategy, EmptyListStrategy)
diff --git a/pypy/objspace/std/test/test_rangeobject.py b/pypy/objspace/std/test/test_rangeobject.py
--- a/pypy/objspace/std/test/test_rangeobject.py
+++ b/pypy/objspace/std/test/test_rangeobject.py
@@ -44,12 +44,9 @@
 
     def test_empty_range(self):
         r = range(10, 10)
-        if not self.SORT_FORCES_LISTS:
-            r.sort(reverse=True)
         assert len(r) == 0
         assert list(reversed(r)) == []
         assert r[:] == []
-        assert self.not_forced(r)
 
     def test_repr(self):
         r = range(5)
@@ -73,23 +70,6 @@
         r.reverse()
         assert r == [2, 1, 1]
 
-    def test_sort(self):
-        if self.SORT_FORCES_LISTS:
-            skip("sort() forces these lists")
-        r = range(10, -1, -1)
-        r.sort()
-        assert self.not_forced(r)
-        assert r == range(11)
-        r = range(11)
-        r.sort(reverse=True)
-        assert self.not_forced(r)
-        assert r == range(10, -1, -1)
-        r = range(100)
-        r[0] = 999
-        assert not self.not_forced(r)
-        r.sort()
-        assert r == range(1, 100) + [999]
-
     def test_pop(self):
         r = range(10)
         res = r.pop()


More information about the pypy-commit mailing list