Skip to content

Commit 0a581a1

Browse files
committed
Make itertools.islice safe in the FT build
1 parent 266247c commit 0a581a1

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Lib/test/test_free_threading/test_itertools.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import unittest
2-
from itertools import accumulate, batched, chain, combinations_with_replacement, cycle, permutations, zip_longest
2+
from itertools import accumulate, batched, chain, combinations_with_replacement, cycle, islice, permutations, zip_longest
33
from test.support import threading_helper
44

55

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

58+
@threading_helper.reap_threads
59+
def test_islice(self):
60+
number_of_iterations = 6
61+
for _ in range(number_of_iterations):
62+
it = islice(tuple(range(10)), 1, 8, 2)
63+
threading_helper.run_concurrently(work_iterator, nthreads=10, args=[it])
64+
5865
@threading_helper.reap_threads
5966
def test_permutations(self):
6067
number_of_iterations = 6

Modules/itertoolsmodule.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ islice_traverse(PyObject *op, visitproc visit, void *arg)
16241624
}
16251625

16261626
static PyObject *
1627-
islice_next(PyObject *op)
1627+
islice_next_lock_held(PyObject *op)
16281628
{
16291629
isliceobject *lz = isliceobject_CAST(op);
16301630
PyObject *item;
@@ -1663,6 +1663,16 @@ islice_next(PyObject *op)
16631663
return NULL;
16641664
}
16651665

1666+
static PyObject *
1667+
islice_next(PyObject *op)
1668+
{
1669+
PyObject *result;
1670+
Py_BEGIN_CRITICAL_SECTION(op);
1671+
result = islice_next_lock_held(op);
1672+
Py_END_CRITICAL_SECTION();
1673+
return result;
1674+
}
1675+
16661676
PyDoc_STRVAR(islice_doc,
16671677
"islice(iterable, stop) --> islice object\n\
16681678
islice(iterable, start, stop[, step]) --> islice object\n\

0 commit comments

Comments
 (0)