Author: Lukas Diekmann <[email protected]>
Branch: list-strategies
Changeset: r47515:1930688ca3d8
Date: 2011-07-12 13:21 +0200
http://bitbucket.org/pypy/pypy/changeset/1930688ca3d8/
Log: added fast path for empty slice; added tests for lists with range
optimization
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
@@ -524,7 +524,6 @@
assert stop >= 0
sublist = l[start:stop]
storage = self.cast_to_void_star(sublist)
- #XXX check of empty list?
return W_ListObject.from_storage_and_strategy(self.space, storage,
self)
else:
subitems_w = [None] * length
@@ -829,6 +828,10 @@
length = w_list.length()
start, stop, step, slicelength = w_slice.indices4(space, length)
assert slicelength >= 0
+ if slicelength == 0:
+ strategy = space.fromcache(EmptyListStrategy)
+ storage = strategy.cast_to_void_star(None)
+ return W_ListObject.from_storage_and_strategy(space, storage, strategy)
return w_list.getslice(start, stop, step, slicelength)
def getslice__List_ANY_ANY(space, w_list, w_start, w_stop):
diff --git a/pypy/objspace/std/test/test_listobject.py
b/pypy/objspace/std/test/test_listobject.py
--- a/pypy/objspace/std/test/test_listobject.py
+++ b/pypy/objspace/std/test/test_listobject.py
@@ -813,6 +813,42 @@
l.__delslice__(0, 2)
assert l == [3, 4]
+class AppTestForRangeLists(AppTestW_ListObject):
+
+ def setup_class(cls):
+ cls.space = gettestobjspace(**{"objspace.std.withrangelist" :
+ True})
+
+ def test_range_simple_backwards(self):
+ x = range(5,1)
+ assert x == []
+
+ def test_range_big_start(self):
+ x = range(1,10)
+ x[22:0:-1] == range(1,10)
+
+ def test_range_list_invalid_slice(self):
+ x = [1,2,3,4]
+ assert x[10:0] == []
+ assert x[10:0:None] == []
+
+ x = range(1,5)
+ assert x[10:0] == []
+ assert x[10:0:None] == []
+
+ assert x[0:22] == [1,2,3,4]
+ assert x[-1:10] == [4]
+
+ assert x[0:22:None] == [1,2,3,4]
+ assert x[-1:10:None] == [4]
+
+ def test_range_backwards(self):
+ x = range(1,10)
+ assert x[22:-10] == []
+ assert x[22:-10:-1] == [9,8,7,6,5,4,3,2,1]
+ assert x[10:3:-1] == [9,8,7,6,5]
+ assert x[10:3:-2] == [9,7,5]
+ assert x[1:5:-1] == []
class AppTestListFastSubscr:
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit