if (likely(PyList_CheckExact(L))) {
Py_ssize_t size = PyList_GET_SIZE(L);
if (likely(size > (((PyListObject*)L)->allocated >> 1))) {
- if (ix < 0) {
- ix += size;
+ Py_ssize_t cix = ix;
+ if (cix < 0) {
+ cix += size;
}
- if (likely(0 <= ix && ix < size)) {
- PyObject* v = PyList_GET_ITEM(L, ix);
+ if (likely(0 <= cix && cix < size)) {
+ PyObject* v = PyList_GET_ITEM(L, cix);
Py_SIZE(L) -= 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;
}
}
[]
>>> simple_pop(L)
Traceback (most recent call last):
- ...
IndexError: pop from empty list
>>> simple_pop(A())
[]
>>> simple_pop_typed(L)
Traceback (most recent call last):
- ...
IndexError: pop from empty list
"""
return L.pop()
>>> L = list(range(10))
>>> index_pop(L, 2)
2
+ >>> index_pop(L, -10)
+ Traceback (most recent call last):
+ IndexError: pop index out of range
>>> index_pop(L, -2)
8
>>> L
[0, 1, 3, 4, 5, 6, 7, 9]
>>> index_pop(L, 100)
Traceback (most recent call last):
- ...
IndexError: pop index out of range
>>> index_pop(L, -100)
Traceback (most recent call last):
- ...
IndexError: pop index out of range
>>> while L:
>>> index_pop(L, 0)
Traceback (most recent call last):
- ...
IndexError: pop from empty list
>>> index_pop(A(), 3)
[0, 1, 3, 4, 5, 6, 7, 9]
>>> index_pop_typed(L, 100)
Traceback (most recent call last):
- ...
IndexError: pop index out of range
>>> index_pop_typed(L, -100)
Traceback (most recent call last):
- ...
IndexError: pop index out of range
>>> while L:
>>> index_pop_typed(L, 0)
Traceback (most recent call last):
- ...
IndexError: pop from empty list
"""
return L.pop(i)
>>> index_pop_literal(L)
Traceback (most recent call last):
- ...
IndexError: pop from empty list
"""
return L.pop(0)