diff options
author | Raymond Hettinger <python@rcn.com> | 2004-07-09 04:10:20 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-07-09 04:10:20 (GMT) |
commit | a435c53e133546adb4a7d13a73f9b688bf1a6078 (patch) | |
tree | 588fbfb2a1aaa1a6c64309ecb25d229148b3576b /Modules | |
parent | 2b2795ac6d20ad9171eb083a9e3bcf8eeaa4a889 (diff) | |
download | cpython-a435c53e133546adb4a7d13a73f9b688bf1a6078.zip cpython-a435c53e133546adb4a7d13a73f9b688bf1a6078.tar.gz cpython-a435c53e133546adb4a7d13a73f9b688bf1a6078.tar.bz2 |
* balance the left/right search for getitem.
* use assertions instead of tests after internal calls that can't fail.
* expand test coverage
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/collectionsmodule.c | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/Modules/collectionsmodule.c b/Modules/collectionsmodule.c index da276ce..367b444 100644 --- a/Modules/collectionsmodule.c +++ b/Modules/collectionsmodule.c @@ -245,7 +245,7 @@ deque_extendleft(dequeobject *deque, PyObject *iterable) deque->leftblock->data[deque->leftindex] = item; } Py_DECREF(it); - if (PyErr_Occurred()) + if (PyErr_Occurred()) return NULL; Py_RETURN_NONE; } @@ -274,8 +274,7 @@ deque_rotate(dequeobject *deque, PyObject *args) for (i=0 ; i<n ; i++) { item = deque_pop(deque, NULL); - if (item == NULL) - return NULL; + assert (item != NULL); rv = deque_appendleft(deque, item); Py_DECREF(item); if (rv == NULL) @@ -284,8 +283,7 @@ deque_rotate(dequeobject *deque, PyObject *args) } for (i=0 ; i>n ; i--) { item = deque_popleft(deque, NULL); - if (item == NULL) - return NULL; + assert (item != NULL); rv = deque_append(deque, item); Py_DECREF(item); if (rv == NULL) @@ -311,8 +309,7 @@ deque_clear(dequeobject *deque) while (deque_len(deque)) { item = deque_pop(deque, NULL); - if (item == NULL) - return -1; + assert (item != NULL); Py_DECREF(item); } assert(deque->leftblock == deque->rightblock && @@ -388,8 +385,7 @@ deque_del_item(dequeobject *deque, int i) Py_DECREF(item); item = deque_popleft(deque, NULL); - if (item == NULL) - goto fail; + assert (item != NULL); Py_DECREF(item); item = deque_rotate(deque, plus_i); @@ -409,9 +405,9 @@ deque_ass_item(dequeobject *deque, int i, PyObject *v) { PyObject *old_value; block *b; - int n; + int n, len=deque->len, halflen=(len+1)>>1, index=i; - if (i < 0 || i >= deque->len) { + if (i < 0 || i >= len) { PyErr_SetString(PyExc_IndexError, "deque index out of range"); return -1; @@ -422,12 +418,12 @@ deque_ass_item(dequeobject *deque, int i, PyObject *v) i += deque->leftindex; n = i / BLOCKLEN; i %= BLOCKLEN; - if (i < (deque->len >> 1)) { + if (index <= halflen) { b = deque->leftblock; while (n--) b = b->rightlink; } else { - n = (deque->leftindex + deque->len - 1) / BLOCKLEN - n; + n = (deque->leftindex + len - 1) / BLOCKLEN - n; b = deque->rightblock; while (n--) b = b->leftlink; @@ -442,8 +438,10 @@ deque_ass_item(dequeobject *deque, int i, PyObject *v) static PyObject * deque_clearmethod(dequeobject *deque) { - if (deque_clear(deque) == -1) - return NULL; + int rv; + + rv = deque_clear(deque); + assert (rv != -1); Py_RETURN_NONE; } |