summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-10-09 16:02:18 (GMT)
committerRaymond Hettinger <python@rcn.com>2004-10-09 16:02:18 (GMT)
commitdcb9d9467c93f9a97067a4c01d2838452551e4b2 (patch)
treec381172e1b7595992ff29ac0b574f9951bb87184 /Modules
parent7126976ee80ff10cff25a41411274b03c62195c2 (diff)
downloadcpython-dcb9d9467c93f9a97067a4c01d2838452551e4b2.zip
cpython-dcb9d9467c93f9a97067a4c01d2838452551e4b2.tar.gz
cpython-dcb9d9467c93f9a97067a4c01d2838452551e4b2.tar.bz2
Simplify delitem() code by calling rotate() directly instead of using
arguments passed through tuples.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/collectionsmodule.c58
1 files changed, 23 insertions, 35 deletions
diff --git a/Modules/collectionsmodule.c b/Modules/collectionsmodule.c
index a766eec..f9eac8e 100644
--- a/Modules/collectionsmodule.c
+++ b/Modules/collectionsmodule.c
@@ -310,17 +310,14 @@ deque_extendleft(dequeobject *deque, PyObject *iterable)
PyDoc_STRVAR(extendleft_doc,
"Extend the left side of the deque with elements from the iterable");
-static PyObject *
-deque_rotate(dequeobject *deque, PyObject *args)
+static int
+_deque_rotate(dequeobject *deque, int n)
{
- int i, n=1, len=deque->len, halflen=(len+1)>>1;
+ int i, len=deque->len, halflen=(len+1)>>1;
PyObject *item, *rv;
- if (!PyArg_ParseTuple(args, "|i:rotate", &n))
- return NULL;
-
if (len == 0)
- Py_RETURN_NONE;
+ return 0;
if (n > halflen || n < -halflen) {
n %= len;
if (n > halflen)
@@ -335,7 +332,7 @@ deque_rotate(dequeobject *deque, PyObject *args)
rv = deque_appendleft(deque, item);
Py_DECREF(item);
if (rv == NULL)
- return NULL;
+ return -1;
Py_DECREF(rv);
}
for (i=0 ; i>n ; i--) {
@@ -344,10 +341,22 @@ deque_rotate(dequeobject *deque, PyObject *args)
rv = deque_append(deque, item);
Py_DECREF(item);
if (rv == NULL)
- return NULL;
+ return -1;
Py_DECREF(rv);
}
- Py_RETURN_NONE;
+ return 0;
+}
+
+static PyObject *
+deque_rotate(dequeobject *deque, PyObject *args)
+{
+ int n=1;
+
+ if (!PyArg_ParseTuple(args, "|i:rotate", &n))
+ return NULL;
+ if (_deque_rotate(deque, n) == 0)
+ Py_RETURN_NONE;
+ return NULL;
}
PyDoc_STRVAR(rotate_doc,
@@ -424,38 +433,17 @@ 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;
+ PyObject *item;
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);
+ if (_deque_rotate(deque, -i) == -1)
+ return -1;
item = deque_popleft(deque, NULL);
assert (item != NULL);
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;
+ return _deque_rotate(deque, i);
}
static int