Skip to content
Merged
Show file tree
Hide file tree
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
[3.14] gh-123471: Make concurrent iteration over `itertools.permutati…
…ons` and `itertools.combinations_with_replacement` thread-safe (gh-144402)

(cherry picked from commit 009c8c0)

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
  • Loading branch information
eendebakpt authored and nascheme committed Jul 16, 2026
commit 98af9225dd6d49f0450544e6e09ee56fbc9e9153
24 changes: 24 additions & 0 deletions Lib/test/test_free_threading/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from threading import Thread, Barrier
from itertools import (
chain,
combinations_with_replacement,
cycle,
permutations,
tee,
)
from test.support import threading_helper
Expand All @@ -11,6 +13,14 @@
threading_helper.requires_working_threading(module=True)


def work_iterator(it):
while True:
try:
next(it)
except StopIteration:
break


class ItertoolsThreading(unittest.TestCase):

@threading_helper.reap_threads
Expand Down Expand Up @@ -65,6 +75,20 @@ def work(it):

barrier.reset()

@threading_helper.reap_threads
def test_combinations_with_replacement(self):
number_of_iterations = 6
for _ in range(number_of_iterations):
it = combinations_with_replacement(tuple(range(2)), 2)
threading_helper.run_concurrently(work_iterator, nthreads=6, args=[it])

@threading_helper.reap_threads
def test_permutations(self):
number_of_iterations = 6
for _ in range(number_of_iterations):
it = permutations(tuple(range(2)), 2)
threading_helper.run_concurrently(work_iterator, nthreads=6, args=[it])



class TestTeeConcurrent(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make concurrent iteration over :class:`itertools.combinations_with_replacement` and :class:`itertools.permutations` safe under free-threading.
24 changes: 22 additions & 2 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2684,7 +2684,7 @@ cwr_traverse(PyObject *op, visitproc visit, void *arg)
}

static PyObject *
cwr_next(PyObject *op)
cwr_next_lock_held(PyObject *op)
{
cwrobject *co = cwrobject_CAST(op);
PyObject *elem;
Expand Down Expand Up @@ -2763,6 +2763,16 @@ cwr_next(PyObject *op)
return NULL;
}

static PyObject *
cwr_next(PyObject *op)
{
PyObject *result;
Py_BEGIN_CRITICAL_SECTION(op);
result = cwr_next_lock_held(op);
Py_END_CRITICAL_SECTION()
return result;
}

static PyMethodDef cwr_methods[] = {
{"__sizeof__", cwr_sizeof, METH_NOARGS, sizeof_doc},
{NULL, NULL} /* sentinel */
Expand Down Expand Up @@ -2943,7 +2953,7 @@ permutations_traverse(PyObject *op, visitproc visit, void *arg)
}

static PyObject *
permutations_next(PyObject *op)
permutations_next_lock_held(PyObject *op)
{
permutationsobject *po = permutationsobject_CAST(op);
PyObject *elem;
Expand Down Expand Up @@ -3033,6 +3043,16 @@ permutations_next(PyObject *op)
return NULL;
}

static PyObject *
permutations_next(PyObject *op)
{
PyObject *result;
Py_BEGIN_CRITICAL_SECTION(op);
result = permutations_next_lock_held(op);
Py_END_CRITICAL_SECTION()
return result;
}

static PyMethodDef permuations_methods[] = {
{"__sizeof__", permutations_sizeof, METH_NOARGS, sizeof_doc},
{NULL, NULL} /* sentinel */
Expand Down