Skip to content

gh-142732: Prevent reentrancy in itertools.zip_longest - #150675

Open
brijkapadia wants to merge 5 commits into
python:mainfrom
brijkapadia:itertools-reentrancy
Open

gh-142732: Prevent reentrancy in itertools.zip_longest#150675
brijkapadia wants to merge 5 commits into
python:mainfrom
brijkapadia:itertools-reentrancy

Conversation

@brijkapadia

@brijkapadiabrijkapadia commented May 31, 2026

Copy link
Copy Markdown
Contributor

Adds a check in itertools.zip_longest that raises a ValueError when a reentrancy is detected.

Adds a check in `functools.zip_longest` that raises a `ValueError` when a reentrancy is detected.
@brijkapadiabrijkapadia changed the title gh-142732: Prevent reentrancy in functools.zip_longestgh-142732: Prevent reentrancy in itertools.zip_longestMay 31, 2026
return NULL;
if (lz->numactive == 0)
if (lz->running == 1) {
PyErr_SetString(PyExc_ValueError,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

itertools.tee raises a RuntimeError, but generators raise a ValueError on re-entrant calls. In #150589 I picked RuntimeError for pairwise re-entrant calls.

I have no strong opinion on the type of error, but lets make it an informed decision and then be consistent. @rhettinger

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eendebakpt@rhettinger Has there been any update yet on what error is best to raise?

@@ -0,0 +1 @@
Now :func:`itertools.zip_longest` raises an :exc:`ValueError` when a reentrancy is detected.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a behaviour change. I think we should add an entry to the 3.16 whatsnew (https://docs.python.org/3.16/whatsnew/3.16.html). Can be done in a follow PR once we know which other iterators are changed.

Comment threadModules/itertoolsmodule.c Outdated
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@brijkapadia@eendebakpt