mirror of
https://github.com/python/cpython.git
synced 2025-11-02 11:08:57 +00:00
* 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.
This commit is contained in:
parent
2012f174ea
commit
f0c00241ae
1 changed files with 30 additions and 62 deletions
|
|
@ -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;
|
||||||
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)
|
||||||
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
|
||||||
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)
|
||||||
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,39 +740,28 @@ 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;
|
|
||||||
|
|
||||||
for (i=0 ; i<numargs ; i++) {
|
for (i=0 ; i<numargs ; i++) {
|
||||||
val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i));
|
val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i));
|
||||||
if (val == NULL) {
|
if (val == NULL) {
|
||||||
Py_DECREF(argtuple);
|
Py_DECREF(argtuple);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
PyTuple_SET_ITEM(argtuple, i, val);
|
|
||||||
}
|
}
|
||||||
return argtuple;
|
PyTuple_SET_ITEM(argtuple, i, val);
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
return PyObject_Call(lz->func, argtuple, NULL);
|
|
||||||
}
|
}
|
||||||
|
if (lz->func == Py_None)
|
||||||
|
return argtuple;
|
||||||
|
result = PyObject_Call(lz->func, argtuple, NULL);
|
||||||
|
Py_DECREF(argtuple);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
|
@ -1331,32 +1302,29 @@ 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);
|
for (i=0 ; i < tuplesize ; i++) {
|
||||||
lz->result = result;
|
it = PyTuple_GET_ITEM(lz->ittuple, i);
|
||||||
}
|
item = PyIter_Next(it);
|
||||||
assert(lz->result == result);
|
if (item == NULL) {
|
||||||
assert(result->ob_refcnt == 2);
|
Py_DECREF(result);
|
||||||
|
return NULL;
|
||||||
for (i=0 ; i < tuplesize ; i++) {
|
}
|
||||||
it = PyTuple_GET_ITEM(lz->ittuple, i);
|
PyTuple_SET_ITEM(result, i, item);
|
||||||
item = PyIter_Next(it);
|
|
||||||
if (item == NULL) {
|
|
||||||
Py_DECREF(result);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
PyTuple_SET_ITEM(result, i, item);
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue