summaryrefslogtreecommitdiffstats
path: root/Modules/itertoolsmodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/itertoolsmodule.c')
-rw-r--r--Modules/itertoolsmodule.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 48e6c35..2ac5ab2 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -17,6 +17,7 @@ typedef struct {
PyObject *tgtkey;
PyObject *currkey;
PyObject *currvalue;
+ const void *currgrouper; /* borrowed reference */
} groupbyobject;
static PyTypeObject groupby_type;
@@ -77,6 +78,7 @@ groupby_next(groupbyobject *gbo)
{
PyObject *newvalue, *newkey, *r, *grouper;
+ gbo->currgrouper = NULL;
/* skip to next iteration group */
for (;;) {
if (gbo->currkey == NULL)
@@ -255,6 +257,7 @@ _grouper_create(groupbyobject *parent, PyObject *tgtkey)
Py_INCREF(parent);
igo->tgtkey = tgtkey;
Py_INCREF(tgtkey);
+ parent->currgrouper = igo; /* borrowed reference */
PyObject_GC_Track(igo);
return (PyObject *)igo;
@@ -284,6 +287,8 @@ _grouper_next(_grouperobject *igo)
PyObject *newvalue, *newkey, *r;
int rcmp;
+ if (gbo->currgrouper != igo)
+ return NULL;
if (gbo->currvalue == NULL) {
newvalue = PyIter_Next(gbo->it);
if (newvalue == NULL)
@@ -321,6 +326,9 @@ _grouper_next(_grouperobject *igo)
static PyObject *
_grouper_reduce(_grouperobject *lz)
{
+ if (((groupbyobject *)lz->parent)->currgrouper != lz) {
+ return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
+ }
return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->parent, lz->tgtkey);
}