Skip to content

Need to prevent re-entering into IterNext of itertools.tee  #3891

@Snowapril

Description

@Snowapril

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

Relevant Test Case

  • test_itertools.py test_tee_reenter

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions