Commit f0c00241 authored by Raymond Hettinger's avatar Raymond Hettinger

* Eliminated tuple re-use in imap(). Doing it correctly made the code

  too hard to read.
* Simplified previous changes to izip() to make it easier to read.
parent 2012f174
...@@ -637,7 +637,6 @@ PyTypeObject starmap_type = { ...@@ -637,7 +637,6 @@ PyTypeObject starmap_type = {
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
PyObject *iters; PyObject *iters;
PyObject *argtuple;
PyObject *func; PyObject *func;
} imapobject; } imapobject;
...@@ -646,7 +645,7 @@ PyTypeObject imap_type; ...@@ -646,7 +645,7 @@ PyTypeObject imap_type;
static PyObject * static PyObject *
imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ {
PyObject *it, *iters, *argtuple, *func; PyObject *it, *iters, *func;
imapobject *lz; imapobject *lz;
int numargs, i; int numargs, i;
...@@ -661,34 +660,23 @@ imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -661,34 +660,23 @@ imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (iters == NULL) if (iters == NULL)
return NULL; return NULL;
argtuple = PyTuple_New(numargs-1);
if (argtuple == NULL) {
Py_DECREF(iters);
return NULL;
}
for (i=1 ; i<numargs ; i++) { for (i=1 ; i<numargs ; i++) {
/* Get iterator. */ /* Get iterator. */
it = PyObject_GetIter(PyTuple_GET_ITEM(args, i)); it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
if (it == NULL) { if (it == NULL) {
Py_DECREF(argtuple);
Py_DECREF(iters); Py_DECREF(iters);
return NULL; return NULL;
} }
PyTuple_SET_ITEM(iters, i-1, it); PyTuple_SET_ITEM(iters, i-1, it);
Py_INCREF(Py_None);
PyTuple_SET_ITEM(argtuple, i-1, Py_None);
} }
/* create imapobject structure */ /* create imapobject structure */
lz = (imapobject *)type->tp_alloc(type, 0); lz = (imapobject *)type->tp_alloc(type, 0);
if (lz == NULL) { if (lz == NULL) {
Py_DECREF(argtuple);
Py_DECREF(iters); Py_DECREF(iters);
return NULL; return NULL;
} }
lz->iters = iters; lz->iters = iters;
lz->argtuple = argtuple;
func = PyTuple_GET_ITEM(args, 0); func = PyTuple_GET_ITEM(args, 0);
Py_INCREF(func); Py_INCREF(func);
lz->func = func; lz->func = func;
...@@ -700,7 +688,6 @@ static void ...@@ -700,7 +688,6 @@ static void
imap_dealloc(imapobject *lz) imap_dealloc(imapobject *lz)
{ {
PyObject_GC_UnTrack(lz); PyObject_GC_UnTrack(lz);
Py_XDECREF(lz->argtuple);
Py_XDECREF(lz->iters); Py_XDECREF(lz->iters);
Py_XDECREF(lz->func); Py_XDECREF(lz->func);
lz->ob_type->tp_free(lz); lz->ob_type->tp_free(lz);
...@@ -716,11 +703,6 @@ imap_traverse(imapobject *lz, visitproc visit, void *arg) ...@@ -716,11 +703,6 @@ imap_traverse(imapobject *lz, visitproc visit, void *arg)
if (err) if (err)
return err; return err;
} }
if (lz->argtuple) {
err = visit(lz->argtuple, arg);
if (err)
return err;
}
if (lz->func) { if (lz->func) {
err = visit(lz->func, arg); err = visit(lz->func, arg);
if (err) if (err)
...@@ -758,11 +740,11 @@ static PyObject * ...@@ -758,11 +740,11 @@ static PyObject *
imap_next(imapobject *lz) imap_next(imapobject *lz)
{ {
PyObject *val; PyObject *val;
PyObject *argtuple=lz->argtuple; PyObject *argtuple;
PyObject *result;
int numargs, i; int numargs, i;
numargs = PyTuple_Size(lz->iters); numargs = PyTuple_Size(lz->iters);
if (lz->func == Py_None) {
argtuple = PyTuple_New(numargs); argtuple = PyTuple_New(numargs);
if (argtuple == NULL) if (argtuple == NULL)
return NULL; return NULL;
...@@ -775,22 +757,11 @@ imap_next(imapobject *lz) ...@@ -775,22 +757,11 @@ imap_next(imapobject *lz)
} }
PyTuple_SET_ITEM(argtuple, i, val); PyTuple_SET_ITEM(argtuple, i, val);
} }
if (lz->func == Py_None)
return argtuple; return argtuple;
} else { result = PyObject_Call(lz->func, argtuple, NULL);
if (argtuple->ob_refcnt > 1) { Py_DECREF(argtuple);
argtuple = PyTuple_New(numargs); return result;
if (argtuple == NULL)
return NULL;
}
for (i=0 ; i<numargs ; i++) {
val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i));
if (val == NULL)
return NULL;
Py_DECREF(PyTuple_GET_ITEM(argtuple, i));
PyTuple_SET_ITEM(argtuple, i, val);
}
return PyObject_Call(lz->func, argtuple, NULL);
}
} }
static PyObject * static PyObject *
...@@ -1331,24 +1302,20 @@ izip_next(izipobject *lz) ...@@ -1331,24 +1302,20 @@ izip_next(izipobject *lz)
PyObject *it; PyObject *it;
PyObject *item; PyObject *item;
assert(result->ob_refcnt >= 1);
if (result->ob_refcnt == 1) { if (result->ob_refcnt == 1) {
for (i=0 ; i < tuplesize ; i++) { for (i=0 ; i < tuplesize ; i++) {
it = PyTuple_GET_ITEM(lz->ittuple, i);
item = PyIter_Next(it);
if (item == NULL)
return NULL;
Py_DECREF(PyTuple_GET_ITEM(result, i)); Py_DECREF(PyTuple_GET_ITEM(result, i));
PyTuple_SET_ITEM(result, i, NULL); PyTuple_SET_ITEM(result, i, item);
} }
Py_INCREF(result); Py_INCREF(result);
} else { } else {
Py_DECREF(result);
result = PyTuple_New(tuplesize); result = PyTuple_New(tuplesize);
if (result == NULL) if (result == NULL)
return NULL; return NULL;
Py_INCREF(result);
lz->result = result;
}
assert(lz->result == result);
assert(result->ob_refcnt == 2);
for (i=0 ; i < tuplesize ; i++) { for (i=0 ; i < tuplesize ; i++) {
it = PyTuple_GET_ITEM(lz->ittuple, i); it = PyTuple_GET_ITEM(lz->ittuple, i);
item = PyIter_Next(it); item = PyIter_Next(it);
...@@ -1358,6 +1325,7 @@ izip_next(izipobject *lz) ...@@ -1358,6 +1325,7 @@ izip_next(izipobject *lz)
} }
PyTuple_SET_ITEM(result, i, item); PyTuple_SET_ITEM(result, i, item);
} }
}
return result; return result;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment