-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Open
Labels
Description
Feature
As itertools.tee
generates n
independent iterators from a single iterable object, each IterNext
of itertools.tee
object will access the shallow-copy
of PyItertoolsTeeData
.
We need to prevent re-entering get_item
method of PyItertoolsTeeData
during iteration for keeping PyItertoolsTeeData
thread-safety.
CPython use running
variable whether the current entering is re-entering or first-entering like below code
// In a middle of get_item method of PyItertoolsTeeData
if (tdo->running) {
PyErr_SetString(PyExc_RuntimeError,
"cannot re-enter the tee iterator");
return NULL;
}
tdo->running = 1;
value = PyIter_Next(tdo->it);
tdo->running = 0;
Python Documentation
- https://bugs.python.org/issue34410
- bpo-34410: Fix a crash in the tee iterator when re-enter it. python/cpython#15625
- https://github.com/python/cpython/blob/3.10/Modules/itertoolsmodule.c#L577-L646
Relevant Test Case
test_itertools.py
test_tee_reenter