Commit 3377d3e3 authored by Robert Bradshaw's avatar Robert Bradshaw

Fix tuple/list wraparound failure.

Previously, if incrementing by the length failed to make the
index positive, this *incremented* index was passed in an attempt
to raise the correct error.

This fixes #1700.
parent 26c6ecb1
......@@ -282,9 +282,12 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_{{type}}_Fast(PyObject *o, Py_ss
CYTHON_NCP_UNUSED int wraparound,
CYTHON_NCP_UNUSED int boundscheck) {
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
if (wraparound & unlikely(i < 0)) i += Py{{type}}_GET_SIZE(o);
if ((!boundscheck) || likely((0 <= i) & (i < Py{{type}}_GET_SIZE(o)))) {
PyObject *r = Py{{type}}_GET_ITEM(o, i);
Py_ssize_t wrapped_i = i;
if (wraparound & unlikely(i < 0)) {
wrapped_i += Py{{type}}_GET_SIZE(o);
}
if ((!boundscheck) || likely((0 <= wrapped_i) & (wrapped_i < Py{{type}}_GET_SIZE(o)))) {
PyObject *r = Py{{type}}_GET_ITEM(o, wrapped_i);
Py_INCREF(r);
return r;
}
......
......@@ -29,6 +29,9 @@ def index_tuple(tuple t, int i):
>>> index_tuple((1,1,2,3,5), 100)
Traceback (most recent call last):
IndexError: tuple index out of range
>>> index_tuple((1,1,2,3,5), -7)
Traceback (most recent call last):
IndexError: tuple index out of range
>>> index_tuple(None, 0)
Traceback (most recent call last):
TypeError: 'NoneType' object is not subscriptable
......@@ -46,6 +49,9 @@ def index_list(list L, int i):
>>> index_list([2,3,5,7,11,13,17,19], 100)
Traceback (most recent call last):
IndexError: list index out of range
>>> index_list([2,3,5,7,11,13,17,19], -10)
Traceback (most recent call last):
IndexError: list index out of range
>>> index_list(None, 0)
Traceback (most recent call last):
TypeError: 'NoneType' object is not subscriptable
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment