mirror of
https://github.com/python/cpython.git
synced 2025-08-20 08:41:07 +00:00
[3.12] gh-109786: Fix leaks and crash when re-enter itertools.pairwise.__next__() (GH-109788) (GH-112699)
(cherry picked from commit 6ca9d3e017
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
8d1b3c0a70
commit
55896f470b
3 changed files with 85 additions and 2 deletions
|
@ -330,21 +330,30 @@ pairwise_next(pairwiseobject *po)
|
|||
return NULL;
|
||||
}
|
||||
if (old == NULL) {
|
||||
po->old = old = (*Py_TYPE(it)->tp_iternext)(it);
|
||||
old = (*Py_TYPE(it)->tp_iternext)(it);
|
||||
Py_XSETREF(po->old, old);
|
||||
if (old == NULL) {
|
||||
Py_CLEAR(po->it);
|
||||
return NULL;
|
||||
}
|
||||
it = po->it;
|
||||
if (it == NULL) {
|
||||
Py_CLEAR(po->old);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
Py_INCREF(old);
|
||||
new = (*Py_TYPE(it)->tp_iternext)(it);
|
||||
if (new == NULL) {
|
||||
Py_CLEAR(po->it);
|
||||
Py_CLEAR(po->old);
|
||||
Py_DECREF(old);
|
||||
return NULL;
|
||||
}
|
||||
/* Future optimization: Reuse the result tuple as we do in enumerate() */
|
||||
result = PyTuple_Pack(2, old, new);
|
||||
Py_SETREF(po->old, new);
|
||||
Py_XSETREF(po->old, new);
|
||||
Py_DECREF(old);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue