diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 95ef8d79a16533..bc5382af7e98e1 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -729,7 +729,7 @@ static PyObject * tee_fromiterable(PyObject *iterable) { teeobject *to; - PyObject *it = NULL; + PyObject *it; it = PyObject_GetIter(iterable); if (it == NULL) @@ -739,21 +739,22 @@ tee_fromiterable(PyObject *iterable) goto done; } - to = PyObject_GC_New(teeobject, &tee_type); - if (to == NULL) - goto done; - to->dataobj = (teedataobject *)teedataobject_newinternal(it); - if (!to->dataobj) { - PyObject_GC_Del(to); + PyObject *dataobj = teedataobject_newinternal(it); + if (!dataobj) { to = NULL; goto done; } - + to = PyObject_GC_New(teeobject, &tee_type); + if (to == NULL) { + Py_DECREF(dataobj); + goto done; + } + to->dataobj = (teedataobject *)dataobj; to->index = 0; to->weakreflist = NULL; PyObject_GC_Track(to); done: - Py_XDECREF(it); + Py_DECREF(it); return (PyObject *)to; }