Author: Brian Kearns <[email protected]>
Branch:
Changeset: r69695:69b0efd529c2
Date: 2014-03-04 19:49 -0500
http://bitbucket.org/pypy/pypy/changeset/69b0efd529c2/
Log: fix getitem with negative indices on the new SimpleRangeList
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
@@ -142,7 +142,6 @@
class W_ListObject(W_Root):
-
strategy = None
def __init__(self, space, wrappeditems, sizehint=-1):
@@ -1128,10 +1127,13 @@
def _getitem_unwrapped(self, w_list, i):
length = self.unerase(w_list.lstorage)[0]
assert length > 0
- if 0 <= i < length:
- return i
- else:
+ if i < 0:
+ i += length
+ if i < 0:
+ raise IndexError
+ elif i >= length:
raise IndexError
+ return i
@specialize.arg(2)
def _getitems_range(self, w_list, wrap_items):
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
@@ -166,7 +166,6 @@
self.space.setitem(w_lhslist, w_slice, w_rhslist)
assert self.space.unwrap(w_lhslist) == expected
-
test1([5,7,1,4], 1, 3, [9,8], [5,9,8,4])
test1([5,7,1,4], 1, 3, [9], [5,9,4])
test1([5,7,1,4], 1, 3, [9,8,6],[5,9,8,6,4])
@@ -294,6 +293,7 @@
self.space.w_True)
assert self.space.eq_w(self.space.eq(w_list2, w_list3),
self.space.w_False)
+
def test_ne(self):
w = self.space.wrap
@@ -312,6 +312,7 @@
self.space.w_False)
assert self.space.eq_w(self.space.ne(w_list2, w_list3),
self.space.w_True)
+
def test_lt(self):
w = self.space.wrap
@@ -429,6 +430,7 @@
with py.test.raises(ValueError):
intlist.find(w(4), 0, 2)
+
class AppTestW_ListObject(object):
def setup_class(cls):
import platform
@@ -662,7 +664,6 @@
raises(IndexError, "l[1]")
def test_setitem(self):
-
l = []
raises(IndexError, "l[1] = 2")
@@ -861,7 +862,6 @@
raises(TypeError, "[0]*MyInt(3)")
raises(TypeError, "[0]*MyIndex(MyInt(3))")
-
def test_index(self):
c = range(10)
assert c.index(0) == 0
@@ -1318,6 +1318,8 @@
assert ([5] >= [N]) is False
def test_resizelist_hint(self):
+ if self.on_cpython:
+ skip('pypy-only test')
import __pypy__
l2 = []
__pypy__.resizelist_hint(l2, 100)
@@ -1326,6 +1328,8 @@
assert len(l1) == 0
def test_use_method_for_wrong_object(self):
+ if self.on_cpython:
+ skip('pypy-only test')
raises(TypeError, list.append.im_func, 1, 2)
def test_ne_NotImplemented(self):
@@ -1439,7 +1443,20 @@
def test_getitem(self):
l = range(5)
- raises(IndexError, "l[-10]")
+ raises(IndexError, "l[-6]")
+ raises(IndexError, "l[5]")
+ assert l[0] == 0
+ assert l[-1] == 4
+ assert l[-2] == 3
+ assert l[-5] == 0
+
+ l = range(1, 5)
+ raises(IndexError, "l[-5]")
+ raises(IndexError, "l[4]")
+ assert l[0] == 1
+ assert l[-1] == 4
+ assert l[-2] == 3
+ assert l[-4] == 1
def test_append(self):
l = range(5)
@@ -1515,6 +1532,7 @@
notshared = l[:]
assert notshared == []
+
class AppTestListFastSubscr:
spaceconfig = {"objspace.std.optimized_list_getitem": True}
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit