diff options
author | Raymond Hettinger <python@rcn.com> | 2004-05-12 20:55:56 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-05-12 20:55:56 (GMT) |
commit | 0e371f2cb6178de7344c97e4a546703c4bba140e (patch) | |
tree | 52fd07c90da0c60ee0c65fa991f7d27db49f00c3 /Modules/collectionsmodule.c | |
parent | fd3f4fb7b114e177767568da35eee2241a8eda18 (diff) | |
download | cpython-0e371f2cb6178de7344c97e4a546703c4bba140e.zip cpython-0e371f2cb6178de7344c97e4a546703c4bba140e.tar.gz cpython-0e371f2cb6178de7344c97e4a546703c4bba140e.tar.bz2 |
Make sure "del d[n]" is properly supported. Was necessary because the
same method that implements __setitem__ also implements __delitem__.
Also, there were several good use cases (removing items from a queue
and implementing Forth style stack ops).
Diffstat (limited to 'Modules/collectionsmodule.c')
-rw-r--r-- | Modules/collectionsmodule.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Modules/collectionsmodule.c b/Modules/collectionsmodule.c index fc30c99..e49224d 100644 --- a/Modules/collectionsmodule.c +++ b/Modules/collectionsmodule.c @@ -353,6 +353,44 @@ deque_item(dequeobject *deque, int i) } static int +deque_del_item(dequeobject *deque, int i) +{ + PyObject *item=NULL, *minus_i=NULL, *plus_i=NULL; + int rv = -1; + + assert (i >= 0 && i < deque->len); + + minus_i = Py_BuildValue("(i)", -i); + if (minus_i == NULL) + goto fail; + + plus_i = Py_BuildValue("(i)", i); + if (plus_i == NULL) + goto fail; + + item = deque_rotate(deque, minus_i); + if (item == NULL) + goto fail; + Py_DECREF(item); + + item = deque_popleft(deque, NULL); + if (item == NULL) + goto fail; + Py_DECREF(item); + + item = deque_rotate(deque, plus_i); + if (item == NULL) + goto fail; + + rv = 0; +fail: + Py_XDECREF(item); + Py_XDECREF(minus_i); + Py_XDECREF(plus_i); + return rv; +} + +static int deque_ass_item(dequeobject *deque, int i, PyObject *v) { PyObject *old_value; @@ -364,6 +402,9 @@ deque_ass_item(dequeobject *deque, int i, PyObject *v) "deque index out of range"); return -1; } + if (v == NULL) + return deque_del_item(deque, i); + i += deque->leftindex; n = i / BLOCKLEN; i %= BLOCKLEN; |