summaryrefslogtreecommitdiffstats
path: root/Modules/itertoolsmodule.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-01-25 11:24:47 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-01-25 11:24:47 (GMT)
commit339e91d4cb5fcbd622447b5249c4570d35c577f9 (patch)
tree3410e985aef9c6cc94e4678b2b7f3b9372be97ff /Modules/itertoolsmodule.c
parent5070c27a8f1ccc887c5cf60fd9993f0df3234f17 (diff)
parenta3e9128aba49b99451b19b49982b7b48e4f7ffe6 (diff)
downloadcpython-339e91d4cb5fcbd622447b5249c4570d35c577f9.zip
cpython-339e91d4cb5fcbd622447b5249c4570d35c577f9.tar.gz
cpython-339e91d4cb5fcbd622447b5249c4570d35c577f9.tar.bz2
Issue #13454: Fix a crash when deleting an iterator created by itertools.tee()
if all other iterators were very advanced before.
Diffstat (limited to 'Modules/itertoolsmodule.c')
-rw-r--r--Modules/itertoolsmodule.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 9115b67..8d20ea0 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -473,14 +473,31 @@ teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg)
return 0;
}
+static void
+teedataobject_safe_decref(PyObject *obj)
+{
+ while (obj && Py_TYPE(obj) == &teedataobject_type &&
+ Py_REFCNT(obj) == 1) {
+ PyObject *nextlink = ((teedataobject *)obj)->nextlink;
+ ((teedataobject *)obj)->nextlink = NULL;
+ Py_DECREF(obj);
+ obj = nextlink;
+ }
+ Py_XDECREF(obj);
+}
+
static int
teedataobject_clear(teedataobject *tdo)
{
int i;
+ PyObject *tmp;
+
Py_CLEAR(tdo->it);
for (i=0 ; i<tdo->numread ; i++)
Py_CLEAR(tdo->values[i]);
- Py_CLEAR(tdo->nextlink);
+ tmp = tdo->nextlink;
+ tdo->nextlink = NULL;
+ teedataobject_safe_decref(tmp);
return 0;
}
@@ -617,6 +634,8 @@ tee_next(teeobject *to)
if (to->index >= LINKCELLS) {
link = teedataobject_jumplink(to->dataobj);
+ if (link == NULL)
+ return NULL;
Py_DECREF(to->dataobj);
to->dataobj = (teedataobject *)link;
to->index = 0;