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
Copy link
Copy Markdown
Contributor

@brijkapadia brijkapadia commented May 31, 2026

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.
@brijkapadia brijkapadia changed the title gh-142732: Prevent reentrancy in functools.zip_longest gh-142732: Prevent reentrancy in itertools.zip_longest May 31, 2026
Comment thread Modules/itertoolsmodule.c
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

@@ -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 thread Modules/itertoolsmodule.c Outdated
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-05-31-16-58-14.gh-issue-142732.6Q80QA.rst Outdated
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Sign up for free to 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