Commit f00fd1de authored by Stefan Behnel's avatar Stefan Behnel

fix accidental double wrap-around of negative indices in pop() optimisation

parent d45bf003
...@@ -90,14 +90,15 @@ static PyObject* __Pyx_PyObject_PopIndex(PyObject* L, Py_ssize_t ix) { ...@@ -90,14 +90,15 @@ static PyObject* __Pyx_PyObject_PopIndex(PyObject* L, Py_ssize_t ix) {
if (likely(PyList_CheckExact(L))) { if (likely(PyList_CheckExact(L))) {
Py_ssize_t size = PyList_GET_SIZE(L); Py_ssize_t size = PyList_GET_SIZE(L);
if (likely(size > (((PyListObject*)L)->allocated >> 1))) { if (likely(size > (((PyListObject*)L)->allocated >> 1))) {
if (ix < 0) { Py_ssize_t cix = ix;
ix += size; if (cix < 0) {
cix += size;
} }
if (likely(0 <= ix && ix < size)) { if (likely(0 <= cix && cix < size)) {
PyObject* v = PyList_GET_ITEM(L, ix); PyObject* v = PyList_GET_ITEM(L, cix);
Py_SIZE(L) -= 1; Py_SIZE(L) -= 1;
size -= 1; size -= 1;
memmove(&PyList_GET_ITEM(L, ix), &PyList_GET_ITEM(L, ix+1), (size-ix)*sizeof(PyObject*)); memmove(&PyList_GET_ITEM(L, cix), &PyList_GET_ITEM(L, cix+1), (size-cix)*sizeof(PyObject*));
return v; return v;
} }
} }
......
...@@ -24,7 +24,6 @@ def simple_pop(L): ...@@ -24,7 +24,6 @@ def simple_pop(L):
[] []
>>> simple_pop(L) >>> simple_pop(L)
Traceback (most recent call last): Traceback (most recent call last):
...
IndexError: pop from empty list IndexError: pop from empty list
>>> simple_pop(A()) >>> simple_pop(A())
...@@ -50,7 +49,6 @@ def simple_pop_typed(list L): ...@@ -50,7 +49,6 @@ def simple_pop_typed(list L):
[] []
>>> simple_pop_typed(L) >>> simple_pop_typed(L)
Traceback (most recent call last): Traceback (most recent call last):
...
IndexError: pop from empty list IndexError: pop from empty list
""" """
return L.pop() return L.pop()
...@@ -63,17 +61,18 @@ def index_pop(L, int i): ...@@ -63,17 +61,18 @@ def index_pop(L, int i):
>>> L = list(range(10)) >>> L = list(range(10))
>>> index_pop(L, 2) >>> index_pop(L, 2)
2 2
>>> index_pop(L, -10)
Traceback (most recent call last):
IndexError: pop index out of range
>>> index_pop(L, -2) >>> index_pop(L, -2)
8 8
>>> L >>> L
[0, 1, 3, 4, 5, 6, 7, 9] [0, 1, 3, 4, 5, 6, 7, 9]
>>> index_pop(L, 100) >>> index_pop(L, 100)
Traceback (most recent call last): Traceback (most recent call last):
...
IndexError: pop index out of range IndexError: pop index out of range
>>> index_pop(L, -100) >>> index_pop(L, -100)
Traceback (most recent call last): Traceback (most recent call last):
...
IndexError: pop index out of range IndexError: pop index out of range
>>> while L: >>> while L:
...@@ -84,7 +83,6 @@ def index_pop(L, int i): ...@@ -84,7 +83,6 @@ def index_pop(L, int i):
>>> index_pop(L, 0) >>> index_pop(L, 0)
Traceback (most recent call last): Traceback (most recent call last):
...
IndexError: pop from empty list IndexError: pop from empty list
>>> index_pop(A(), 3) >>> index_pop(A(), 3)
...@@ -105,11 +103,9 @@ def index_pop_typed(list L, int i): ...@@ -105,11 +103,9 @@ def index_pop_typed(list L, int i):
[0, 1, 3, 4, 5, 6, 7, 9] [0, 1, 3, 4, 5, 6, 7, 9]
>>> index_pop_typed(L, 100) >>> index_pop_typed(L, 100)
Traceback (most recent call last): Traceback (most recent call last):
...
IndexError: pop index out of range IndexError: pop index out of range
>>> index_pop_typed(L, -100) >>> index_pop_typed(L, -100)
Traceback (most recent call last): Traceback (most recent call last):
...
IndexError: pop index out of range IndexError: pop index out of range
>>> while L: >>> while L:
...@@ -120,7 +116,6 @@ def index_pop_typed(list L, int i): ...@@ -120,7 +116,6 @@ def index_pop_typed(list L, int i):
>>> index_pop_typed(L, 0) >>> index_pop_typed(L, 0)
Traceback (most recent call last): Traceback (most recent call last):
...
IndexError: pop from empty list IndexError: pop from empty list
""" """
return L.pop(i) return L.pop(i)
...@@ -143,7 +138,6 @@ def index_pop_literal(list L): ...@@ -143,7 +138,6 @@ def index_pop_literal(list L):
>>> index_pop_literal(L) >>> index_pop_literal(L)
Traceback (most recent call last): Traceback (most recent call last):
...
IndexError: pop from empty list IndexError: pop from empty list
""" """
return L.pop(0) return L.pop(0)
......
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