[Python-checkins] python/dist/src/Modules collectionsmodule.c, 1.33, 1.34

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sat Oct 9 18:02:21 CEST 2004


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30794

Modified Files:
	collectionsmodule.c 
Log Message:
Simplify delitem() code by calling rotate() directly instead of using
arguments passed through tuples.



Index: collectionsmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/collectionsmodule.c,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -d -r1.33 -r1.34
--- collectionsmodule.c	6 Oct 2004 17:51:54 -0000	1.33
+++ collectionsmodule.c	9 Oct 2004 16:02:18 -0000	1.34
@@ -310,17 +310,14 @@
 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 @@
 		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 @@
 		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 @@
 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



More information about the Python-checkins mailing list