gh-142732: Prevent reentrancy in itertools.zip_longest#150675
Open
brijkapadia wants to merge 5 commits into
Open
gh-142732: Prevent reentrancy in itertools.zip_longest#150675brijkapadia wants to merge 5 commits into
itertools.zip_longest#150675brijkapadia wants to merge 5 commits into
Conversation
Adds a check in `functools.zip_longest` that raises a `ValueError` when a reentrancy is detected.
functools.zip_longestitertools.zip_longest
eendebakpt
reviewed
May 31, 2026
| return NULL; | ||
| if (lz->numactive == 0) | ||
| if (lz->running == 1) { | ||
| PyErr_SetString(PyExc_ValueError, |
Contributor
There was a problem hiding this comment.
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
eendebakpt
reviewed
May 31, 2026
| @@ -0,0 +1 @@ | |||
| Now :func:`itertools.zip_longest` raises an :exc:`ValueError` when a reentrancy is detected. | |||
Contributor
There was a problem hiding this comment.
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.
eendebakpt
reviewed
May 31, 2026
eendebakpt
reviewed
May 31, 2026
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a check in
itertools.zip_longestthat raises aValueErrorwhen a reentrancy is detected.itertools.zip_longest_next/islice/batchedvia re-entrant iterator #142732