Skip to content

Commit 11e727d

Browse files
committed
Make itertools.pairwise safe in the FT build
1 parent 8810175 commit 11e727d

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Modules/itertoolsmodule.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ pairwise_traverse(PyObject *op, visitproc visit, void *arg)
343343
}
344344

345345
static PyObject *
346-
pairwise_next(PyObject *op)
346+
pairwise_next_lock_held(PyObject *op)
347347
{
348348
pairwiseobject *po = pairwiseobject_CAST(op);
349349
PyObject *it = po->it;
@@ -380,7 +380,7 @@ pairwise_next(PyObject *op)
380380
Py_INCREF(result);
381381
PyObject *last_old = PyTuple_GET_ITEM(result, 0);
382382
PyObject *last_new = PyTuple_GET_ITEM(result, 1);
383-
PyTuple_SET_ITEM(result, 0, Py_NewRef(old));
383+
PyTuple_SET_ITEM(result, 0, old);
384384
PyTuple_SET_ITEM(result, 1, Py_NewRef(new));
385385
Py_DECREF(last_old);
386386
Py_DECREF(last_new);
@@ -391,13 +391,25 @@ pairwise_next(PyObject *op)
391391
else {
392392
result = PyTuple_New(2);
393393
if (result != NULL) {
394-
PyTuple_SET_ITEM(result, 0, Py_NewRef(old));
394+
PyTuple_SET_ITEM(result, 0, old);
395395
PyTuple_SET_ITEM(result, 1, Py_NewRef(new));
396396
}
397+
else {
398+
Py_DECREF(old);
399+
}
397400
}
398401

399402
Py_XSETREF(po->old, new);
400-
Py_DECREF(old);
403+
return result;
404+
}
405+
406+
static PyObject *
407+
pairwise_next(PyObject *op)
408+
{
409+
PyObject *result;
410+
Py_BEGIN_CRITICAL_SECTION(op);
411+
result = pairwise_next_lock_held(op);
412+
Py_END_CRITICAL_SECTION()
401413
return result;
402414
}
403415

0 commit comments

Comments
 (0)