diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index adbe7897004..8766d86dd3e 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -938,8 +938,7 @@ deque_reverse(dequeobject *deque, PyObject *unused) Py_ssize_t n = Py_SIZE(deque) >> 1; PyObject *tmp; - n++; - while (--n) { + while (--n >= 0) { /* Validate that pointers haven't met in the middle */ assert(leftblock != rightblock || leftindex < rightindex); CHECK_NOT_END(leftblock); @@ -981,8 +980,7 @@ deque_count(dequeobject *deque, PyObject *v) PyObject *item; int cmp; - n++; - while (--n) { + while (--n >= 0) { CHECK_NOT_END(b); item = b->data[index]; cmp = PyObject_RichCompareBool(item, v, Py_EQ); @@ -1019,8 +1017,7 @@ deque_contains(dequeobject *deque, PyObject *v) PyObject *item; int cmp; - n++; - while (--n) { + while (--n >= 0) { CHECK_NOT_END(b); item = b->data[index]; cmp = PyObject_RichCompareBool(item, v, Py_EQ); @@ -1088,13 +1085,13 @@ deque_index(dequeobject *deque, PyObject **args, Py_ssize_t nargs) } } - n = stop - i + 1; - while (--n) { + n = stop - i; + while (--n >= 0) { CHECK_NOT_END(b); item = b->data[index]; cmp = PyObject_RichCompareBool(item, v, Py_EQ); if (cmp > 0) - return PyLong_FromSsize_t(stop - n); + return PyLong_FromSsize_t(stop - n - 1); if (cmp < 0) return NULL; if (start_state != deque->state) { @@ -1228,16 +1225,14 @@ deque_item(dequeobject *deque, Py_ssize_t i) i = (Py_ssize_t)((size_t) i % BLOCKLEN); if (index < (Py_SIZE(deque) >> 1)) { b = deque->leftblock; - n++; - while (--n) + while (--n >= 0) b = b->rightlink; } else { n = (Py_ssize_t)( ((size_t)(deque->leftindex + Py_SIZE(deque) - 1)) / BLOCKLEN - n); b = deque->rightblock; - n++; - while (--n) + while (--n >= 0) b = b->leftlink; } } @@ -1281,16 +1276,14 @@ deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v) i = (Py_ssize_t)((size_t) i % BLOCKLEN); if (index <= halflen) { b = deque->leftblock; - n++; - while (--n) + while (--n >= 0) b = b->rightlink; } else { n = (Py_ssize_t)( ((size_t)(deque->leftindex + Py_SIZE(deque) - 1)) / BLOCKLEN - n); b = deque->rightblock; - n++; - while (--n) + while (--n >= 0) b = b->leftlink; } Py_INCREF(v);