[pypy-commit] pypy cpyext-avoid-roundtrip: test, fix PySequence_GetItem allows negative indices

mattip pypy.commits at gmail.com
Wed Dec 20 15:10:09 EST 2017


Author: Matti Picus <matti.picus at gmail.com>
Branch: cpyext-avoid-roundtrip
Changeset: r93524:f428c66de212
Date: 2017-12-20 22:09 +0200
http://bitbucket.org/pypy/pypy/changeset/f428c66de212/

Log:	test, fix PySequence_GetItem allows negative indices

diff --git a/pypy/module/cpyext/sequence.py b/pypy/module/cpyext/sequence.py
--- a/pypy/module/cpyext/sequence.py
+++ b/pypy/module/cpyext/sequence.py
@@ -165,6 +165,9 @@
 def PySequence_GetItem(space, w_obj, i):
     """Return the ith element of o, or NULL on failure. This is the equivalent of
     the Python expression o[i]."""
+    if i < 0:
+        l = PySequence_Length(space, w_obj)
+        i += l
     return PySequence_ITEM(space, w_obj, i)
 
 @cpython_api([PyObject], PyObject)
diff --git a/pypy/module/cpyext/test/test_sequence.py b/pypy/module/cpyext/test/test_sequence.py
--- a/pypy/module/cpyext/test/test_sequence.py
+++ b/pypy/module/cpyext/test/test_sequence.py
@@ -184,6 +184,9 @@
         p2 = api.PySequence_GetItem(w1, 1)
         assert p1 == p2
         assert p1.c_ob_refcnt > 1
+        p1 = api.PySequence_GetItem(w1, -1)
+        p2 = api.PySequence_GetItem(w1, 2)
+        assert p1 == p2
 
 
 class AppTestSetObject(AppTestCpythonExtensionBase):


More information about the pypy-commit mailing list