diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 943c1e8607b38f..78af529d5973e4 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -2314,7 +2314,7 @@ combinations_traverse(PyObject *op, visitproc visit, void *arg) } static PyObject * -combinations_next(PyObject *op) +combinations_next_lock_held(PyObject *op) { combinationsobject *co = combinationsobject_CAST(op); PyObject *elem; @@ -2326,9 +2326,6 @@ combinations_next(PyObject *op) Py_ssize_t r = co->r; Py_ssize_t i, j, index; - if (co->stopped) - return NULL; - if (result == NULL) { /* On the first pass, initialize result tuple using the indices */ result = PyTuple_New(r); @@ -2399,6 +2396,21 @@ combinations_next(PyObject *op) return NULL; } +static PyObject * +combinations_next(PyObject *op) +{ + PyObject *result; + + combinationsobject *co = combinationsobject_CAST(op); + if (co->stopped) + return NULL; + + Py_BEGIN_CRITICAL_SECTION(op); + result = combinations_next_lock_held(op); + Py_END_CRITICAL_SECTION(); + return result; +} + static PyMethodDef combinations_methods[] = { {"__sizeof__", combinations_sizeof, METH_NOARGS, sizeof_doc}, {NULL, NULL} /* sentinel */