Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make itertools.pairwise safe in the FT build
  • Loading branch information
eendebakpt committed Feb 4, 2026
commit 11e727dd28aa9f27b6d9e0888152aef3052c41f4
20 changes: 16 additions & 4 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ pairwise_traverse(PyObject *op, visitproc visit, void *arg)
}

static PyObject *
pairwise_next(PyObject *op)
pairwise_next_lock_held(PyObject *op)
{
pairwiseobject *po = pairwiseobject_CAST(op);
PyObject *it = po->it;
Expand Down Expand Up @@ -380,7 +380,7 @@ pairwise_next(PyObject *op)
Py_INCREF(result);
PyObject *last_old = PyTuple_GET_ITEM(result, 0);
PyObject *last_new = PyTuple_GET_ITEM(result, 1);
PyTuple_SET_ITEM(result, 0, Py_NewRef(old));
PyTuple_SET_ITEM(result, 0, old);
PyTuple_SET_ITEM(result, 1, Py_NewRef(new));
Py_DECREF(last_old);
Py_DECREF(last_new);
Expand All @@ -391,13 +391,25 @@ pairwise_next(PyObject *op)
else {
result = PyTuple_New(2);
if (result != NULL) {
PyTuple_SET_ITEM(result, 0, Py_NewRef(old));
PyTuple_SET_ITEM(result, 0, old);
PyTuple_SET_ITEM(result, 1, Py_NewRef(new));
}
else {
Py_DECREF(old);
}
}

Py_XSETREF(po->old, new);
Py_DECREF(old);
return result;
}

static PyObject *
pairwise_next(PyObject *op)
{
PyObject *result;
Py_BEGIN_CRITICAL_SECTION(op);
result = pairwise_next_lock_held(op);
Py_END_CRITICAL_SECTION()
return result;
}

Expand Down