summaryrefslogtreecommitdiff
path: root/Modules/itertoolsmodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/itertoolsmodule.c')
-rw-r--r--Modules/itertoolsmodule.c91
1 files changed, 47 insertions, 44 deletions
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 55d88a8c9f..6ff241e7dd 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -157,15 +157,12 @@ groupby_setstate(groupbyobject *lz, PyObject *state)
PyObject *currkey, *currvalue, *tgtkey;
if (!PyArg_ParseTuple(state, "OOO", &currkey, &currvalue, &tgtkey))
return NULL;
- Py_CLEAR(lz->currkey);
- lz->currkey = currkey;
- Py_INCREF(lz->currkey);
- Py_CLEAR(lz->currvalue);
- lz->currvalue = currvalue;
- Py_INCREF(lz->currvalue);
- Py_CLEAR(lz->tgtkey);
- lz->tgtkey = tgtkey;
- Py_INCREF(lz->tgtkey);
+ Py_INCREF(currkey);
+ Py_XSETREF(lz->currkey, currkey);
+ Py_INCREF(currvalue);
+ Py_XSETREF(lz->currvalue, currvalue);
+ Py_INCREF(tgtkey);
+ Py_XSETREF(lz->tgtkey, tgtkey);
Py_RETURN_NONE;
}
@@ -634,8 +631,7 @@ tee_next(teeobject *to)
link = teedataobject_jumplink(to->dataobj);
if (link == NULL)
return NULL;
- Py_DECREF(to->dataobj);
- to->dataobj = (teedataobject *)link;
+ Py_SETREF(to->dataobj, (teedataobject *)link);
to->index = 0;
}
value = teedataobject_getitem(to->dataobj, to->index);
@@ -746,9 +742,8 @@ tee_setstate(teeobject *to, PyObject *state)
PyErr_SetString(PyExc_ValueError, "Index out of range");
return NULL;
}
- Py_CLEAR(to->dataobj);
- to->dataobj = tdo;
- Py_INCREF(to->dataobj);
+ Py_INCREF(tdo);
+ Py_XSETREF(to->dataobj, tdo);
to->index = index;
Py_RETURN_NONE;
}
@@ -973,9 +968,8 @@ cycle_setstate(cycleobject *lz, PyObject *state)
int firstpass;
if (!PyArg_ParseTuple(state, "Oi", &saved, &firstpass))
return NULL;
- Py_CLEAR(lz->saved);
- lz->saved = saved;
- Py_XINCREF(lz->saved);
+ Py_XINCREF(saved);
+ Py_XSETREF(lz->saved, saved);
lz->firstpass = firstpass != 0;
Py_RETURN_NONE;
}
@@ -1900,12 +1894,10 @@ chain_setstate(chainobject *lz, PyObject *state)
if (! PyArg_ParseTuple(state, "O|O", &source, &active))
return NULL;
- Py_CLEAR(lz->source);
- lz->source = source;
- Py_INCREF(lz->source);
- Py_CLEAR(lz->active);
- lz->active = active;
- Py_XINCREF(lz->active);
+ Py_INCREF(source);
+ Py_XSETREF(lz->source, source);
+ Py_XINCREF(active);
+ Py_XSETREF(lz->active, active);
Py_RETURN_NONE;
}
@@ -1919,7 +1911,7 @@ iterable, until all of the iterables are exhausted.");
PyDoc_STRVAR(chain_from_iterable_doc,
"chain.from_iterable(iterable) --> chain object\n\
\n\
-Alternate chain() contructor taking a single iterable argument\n\
+Alternate chain() constructor taking a single iterable argument\n\
that evaluates lazily.");
static PyMethodDef chain_methods[] = {
@@ -2087,7 +2079,7 @@ product_sizeof(productobject *lz, void *unused)
{
Py_ssize_t res;
- res = sizeof(productobject);
+ res = _PyObject_SIZE(Py_TYPE(lz));
res += PyTuple_GET_SIZE(lz->pools) * sizeof(Py_ssize_t);
return PyLong_FromSsize_t(res);
}
@@ -2261,8 +2253,7 @@ product_setstate(productobject *lz, PyObject *state)
Py_INCREF(element);
PyTuple_SET_ITEM(result, i, element);
}
- Py_CLEAR(lz->result);
- lz->result = result;
+ Py_XSETREF(lz->result, result);
Py_RETURN_NONE;
}
@@ -2418,7 +2409,7 @@ combinations_sizeof(combinationsobject *co, void *unused)
{
Py_ssize_t res;
- res = sizeof(combinationsobject);
+ res = _PyObject_SIZE(Py_TYPE(co));
res += co->r * sizeof(Py_ssize_t);
return PyLong_FromSsize_t(res);
}
@@ -2584,8 +2575,7 @@ combinations_setstate(combinationsobject *lz, PyObject *state)
PyTuple_SET_ITEM(result, i, element);
}
- Py_CLEAR(lz->result);
- lz->result = result;
+ Py_XSETREF(lz->result, result);
Py_RETURN_NONE;
}
@@ -2759,7 +2749,7 @@ cwr_sizeof(cwrobject *co, void *unused)
{
Py_ssize_t res;
- res = sizeof(cwrobject);
+ res = _PyObject_SIZE(Py_TYPE(co));
res += co->r * sizeof(Py_ssize_t);
return PyLong_FromSsize_t(res);
}
@@ -2915,8 +2905,7 @@ cwr_setstate(cwrobject *lz, PyObject *state)
Py_INCREF(element);
PyTuple_SET_ITEM(result, i, element);
}
- Py_CLEAR(lz->result);
- lz->result = result;
+ Py_XSETREF(lz->result, result);
Py_RETURN_NONE;
}
@@ -3108,7 +3097,7 @@ permutations_sizeof(permutationsobject *po, void *unused)
{
Py_ssize_t res;
- res = sizeof(permutationsobject);
+ res = _PyObject_SIZE(Py_TYPE(po));
res += PyTuple_GET_SIZE(po->pool) * sizeof(Py_ssize_t);
res += po->r * sizeof(Py_ssize_t);
return PyLong_FromSsize_t(res);
@@ -3309,8 +3298,7 @@ permutations_setstate(permutationsobject *po, PyObject *state)
Py_INCREF(element);
PyTuple_SET_ITEM(result, i, element);
}
- Py_CLEAR(po->result);
- po->result = result;
+ Py_XSETREF(po->result, result);
Py_RETURN_NONE;
}
@@ -3472,6 +3460,23 @@ accumulate_next(accumulateobject *lz)
static PyObject *
accumulate_reduce(accumulateobject *lz)
{
+ if (lz->total == Py_None) {
+ PyObject *it;
+
+ if (PyType_Ready(&chain_type) < 0)
+ return NULL;
+ if (PyType_Ready(&islice_type) < 0)
+ return NULL;
+ it = PyObject_CallFunction((PyObject *)&chain_type, "(O)O",
+ lz->total, lz->it);
+ if (it == NULL)
+ return NULL;
+ it = PyObject_CallFunction((PyObject *)Py_TYPE(lz), "NO",
+ it, lz->binop ? lz->binop : Py_None);
+ if (it == NULL)
+ return NULL;
+ return Py_BuildValue("O(NiO)", &islice_type, it, 1, Py_None);
+ }
return Py_BuildValue("O(OO)O", Py_TYPE(lz),
lz->it, lz->binop?lz->binop:Py_None,
lz->total?lz->total:Py_None);
@@ -3480,9 +3485,8 @@ accumulate_reduce(accumulateobject *lz)
static PyObject *
accumulate_setstate(accumulateobject *lz, PyObject *state)
{
- Py_CLEAR(lz->total);
- lz->total = state;
- Py_INCREF(lz->total);
+ Py_INCREF(state);
+ Py_XSETREF(lz->total, state);
Py_RETURN_NONE;
}
@@ -3882,7 +3886,7 @@ typedef struct {
fast_mode: when cnt an integer < PY_SSIZE_T_MAX and no step is specified.
- assert(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && long_step==PyInt(1));
+ assert(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && long_step==PyLong(1));
Advances with: cnt += 1
When count hits Y_SSIZE_T_MAX, switch to slow_mode.
@@ -4463,9 +4467,8 @@ zip_longest_reduce(ziplongestobject *lz)
static PyObject *
zip_longest_setstate(ziplongestobject *lz, PyObject *state)
{
- Py_CLEAR(lz->fillvalue);
- lz->fillvalue = state;
- Py_INCREF(lz->fillvalue);
+ Py_INCREF(state);
+ Py_XSETREF(lz->fillvalue, state);
Py_RETURN_NONE;
}
@@ -4480,7 +4483,7 @@ static PyMethodDef zip_longest_methods[] = {
PyDoc_STRVAR(zip_longest_doc,
"zip_longest(iter1 [,iter2 [...]], [fillvalue=None]) --> zip_longest object\n\
\n\
-Return an zip_longest object whose .__next__() method returns a tuple where\n\
+Return a zip_longest object whose .__next__() method returns a tuple where\n\
the i-th element comes from the i-th iterable argument. The .__next__()\n\
method continues until the longest iterable in the argument sequence\n\
is exhausted and then it raises StopIteration. When the shorter iterables\n\