diff options
author | Raymond Hettinger <python@rcn.com> | 2003-02-07 07:26:25 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-02-07 07:26:25 (GMT) |
commit | f0c00241ae8902933ef21ec0718e5c7084b32195 (patch) | |
tree | 721b9b16fb3416fca10fd191a341afcc457529f9 /Modules | |
parent | 2012f174ea305dfab6eefc35add748b87bb239ad (diff) | |
download | cpython-f0c00241ae8902933ef21ec0718e5c7084b32195.zip cpython-f0c00241ae8902933ef21ec0718e5c7084b32195.tar.gz cpython-f0c00241ae8902933ef21ec0718e5c7084b32195.tar.bz2 |
* 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.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/itertoolsmodule.c | 92 |
1 files changed, 30 insertions, 62 deletions
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 69ee3d4..453b4a2 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -637,7 +637,6 @@ PyTypeObject starmap_type = { typedef struct { PyObject_HEAD PyObject *iters; - PyObject *argtuple; PyObject *func; } imapobject; @@ -646,7 +645,7 @@ PyTypeObject imap_type; static PyObject * imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyObject *it, *iters, *argtuple, *func; + PyObject *it, *iters, *func; imapobject *lz; int numargs, i; @@ -661,34 +660,23 @@ imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (iters == NULL) return NULL; - argtuple = PyTuple_New(numargs-1); - if (argtuple == NULL) { - Py_DECREF(iters); - return NULL; - } - for (i=1 ; i<numargs ; i++) { /* Get iterator. */ it = PyObject_GetIter(PyTuple_GET_ITEM(args, i)); if (it == NULL) { - Py_DECREF(argtuple); Py_DECREF(iters); return NULL; } PyTuple_SET_ITEM(iters, i-1, it); - Py_INCREF(Py_None); - PyTuple_SET_ITEM(argtuple, i-1, Py_None); } /* create imapobject structure */ lz = (imapobject *)type->tp_alloc(type, 0); if (lz == NULL) { - Py_DECREF(argtuple); Py_DECREF(iters); return NULL; } lz->iters = iters; - lz->argtuple = argtuple; func = PyTuple_GET_ITEM(args, 0); Py_INCREF(func); lz->func = func; @@ -700,7 +688,6 @@ static void imap_dealloc(imapobject *lz) { PyObject_GC_UnTrack(lz); - Py_XDECREF(lz->argtuple); Py_XDECREF(lz->iters); Py_XDECREF(lz->func); lz->ob_type->tp_free(lz); @@ -716,11 +703,6 @@ imap_traverse(imapobject *lz, visitproc visit, void *arg) if (err) return err; } - if (lz->argtuple) { - err = visit(lz->argtuple, arg); - if (err) - return err; - } if (lz->func) { err = visit(lz->func, arg); if (err) @@ -758,39 +740,28 @@ static PyObject * imap_next(imapobject *lz) { PyObject *val; - PyObject *argtuple=lz->argtuple; + PyObject *argtuple; + PyObject *result; int numargs, i; numargs = PyTuple_Size(lz->iters); - if (lz->func == Py_None) { - argtuple = PyTuple_New(numargs); - if (argtuple == NULL) - return NULL; + argtuple = PyTuple_New(numargs); + if (argtuple == NULL) + return NULL; - for (i=0 ; i<numargs ; i++) { - val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i)); - if (val == NULL) { - Py_DECREF(argtuple); - return NULL; - } - PyTuple_SET_ITEM(argtuple, i, val); - } - return argtuple; - } else { - if (argtuple->ob_refcnt > 1) { - argtuple = PyTuple_New(numargs); - 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); + for (i=0 ; i<numargs ; i++) { + val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i)); + if (val == NULL) { + Py_DECREF(argtuple); + return NULL; } - return PyObject_Call(lz->func, argtuple, NULL); + PyTuple_SET_ITEM(argtuple, i, val); } + if (lz->func == Py_None) + return argtuple; + result = PyObject_Call(lz->func, argtuple, NULL); + Py_DECREF(argtuple); + return result; } static PyObject * @@ -1331,32 +1302,29 @@ izip_next(izipobject *lz) PyObject *it; PyObject *item; - assert(result->ob_refcnt >= 1); if (result->ob_refcnt == 1) { 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)); - PyTuple_SET_ITEM(result, i, NULL); + PyTuple_SET_ITEM(result, i, item); } Py_INCREF(result); } else { - Py_DECREF(result); result = PyTuple_New(tuplesize); if (result == NULL) return NULL; - Py_INCREF(result); - lz->result = result; - } - assert(lz->result == result); - assert(result->ob_refcnt == 2); - - for (i=0 ; i < tuplesize ; i++) { - it = PyTuple_GET_ITEM(lz->ittuple, i); - item = PyIter_Next(it); - if (item == NULL) { - Py_DECREF(result); - return NULL; + for (i=0 ; i < tuplesize ; i++) { + it = PyTuple_GET_ITEM(lz->ittuple, i); + item = PyIter_Next(it); + if (item == NULL) { + Py_DECREF(result); + return NULL; + } + PyTuple_SET_ITEM(result, i, item); } - PyTuple_SET_ITEM(result, i, item); } return result; } |