Skip to content
Open
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 number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from itertools import accumulate, batched, chain, combinations_with_replacement, cycle, permutations, zip_longest
from itertools import accumulate, batched, chain, combinations_with_replacement, cycle, islice, permutations, zip_longest
from test.support import threading_helper


Expand Down Expand Up @@ -55,6 +55,13 @@ def test_combinations_with_replacement(self):
it = combinations_with_replacement(tuple(range(2)), 2)
threading_helper.run_concurrently(work_iterator, nthreads=6, args=[it])

@threading_helper.reap_threads
def test_islice(self):
number_of_iterations = 6
for _ in range(number_of_iterations):
it = islice(tuple(range(10)), 1, 8, 2)
threading_helper.run_concurrently(work_iterator, nthreads=10, args=[it])

@threading_helper.reap_threads
def test_permutations(self):
number_of_iterations = 6
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make concurrent iteration over :class:`itertools.islice` safe under
free-threading.
12 changes: 11 additions & 1 deletion Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@ islice_traverse(PyObject *op, visitproc visit, void *arg)
}

static PyObject *
islice_next(PyObject *op)
islice_next_lock_held(PyObject *op)
{
isliceobject *lz = isliceobject_CAST(op);
PyObject *item;
Expand Down Expand Up @@ -1663,6 +1663,16 @@ islice_next(PyObject *op)
return NULL;
}

static PyObject *
islice_next(PyObject *op)
{
PyObject *result;
Py_BEGIN_CRITICAL_SECTION(op);
result = islice_next_lock_held(op);
Py_END_CRITICAL_SECTION();
return result;
}

PyDoc_STRVAR(islice_doc,
"islice(iterable, stop) --> islice object\n\
islice(iterable, start, stop[, step]) --> islice object\n\
Expand Down
Loading