Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion Lib/test/test_free_threading/test_itertools.py
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
import unittest
from itertools import accumulate, batched, chain, combinations_with_replacement, cycle, permutations
from itertools import accumulate, batched, chain, combinations_with_replacement, cycle, permutations, zip_longest
from test.support import threading_helper


Expand DownExpand Up@@ -62,6 +62,13 @@ def test_permutations(self):
it = permutations(tuple(range(4)), 2)
threading_helper.run_concurrently(work_iterator, nthreads=6, args=[it])

@threading_helper.reap_threads
def test_zip_longest(self):
number_of_iterations = 10
for _ in range(number_of_iterations):
it = zip_longest(list(range(4)), list(range(8)), fillvalue=0)
threading_helper.run_concurrently(work_iterator, nthreads=10, args=[it])


if __name__ == "__main__":
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Make concurrent iteration over :class:`itertools.zip_longest` safe under free-threading.
12 changes: 11 additions & 1 deletion Modules/itertoolsmodule.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -3876,7 +3876,7 @@ zip_longest_traverse(PyObject *op, visitproc visit, void *arg)
}

static PyObject *
zip_longest_next(PyObject *op)
zip_longest_next_lock_held(PyObject *op)
{
ziplongestobject *lz = ziplongestobject_CAST(op);
Py_ssize_t i;
Expand DownExpand Up@@ -3947,6 +3947,16 @@ zip_longest_next(PyObject *op)
return result;
}

static PyObject *
zip_longest_next(PyObject *op)
{
PyObject *result;
Py_BEGIN_CRITICAL_SECTION(op);
result = zip_longest_next_lock_held(op);
Py_END_CRITICAL_SECTION()
return result;
}

PyDoc_STRVAR(zip_longest_doc,
"zip_longest(*iterables, fillvalue=None)\n\
--\n\
Expand Down
Loading