gh-129139: Fix __setstate__ on exhausted list iterators in free-threaded build#146269
Open
ytang928 wants to merge 2 commits intopython:mainfrom
Open
gh-129139: Fix __setstate__ on exhausted list iterators in free-threaded build#146269ytang928 wants to merge 2 commits intopython:mainfrom
ytang928 wants to merge 2 commits intopython:mainfrom
Conversation
…-threaded build In free-threaded builds, exhausted list iterators signal exhaustion by setting it_index to -1 without clearing it_seq to NULL. The existing it_seq != NULL guard in listiter_setstate and listreviter_setstate allowed __setstate__ to revive an exhausted iterator by resetting it_index to a valid value. Add an it_index >= 0 check so that __setstate__ is a no-op on exhausted iterators, consistent with GIL-enabled build behavior.
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.
In free-threaded builds, exhausted list iterators signal exhaustion by
setting
it_indexto-1without clearingit_seqtoNULL(to avoidexpensive atomic refcount operations on the hot path). The existing
it_seq != NULLguard inlistiter_setstateandlistreviter_setstateallowed
__setstate__to revive an exhausted iterator by resettingit_indexto a valid value.This adds an
it_index >= 0check so that__setstate__is a no-op onexhausted iterators, consistent with GIL-enabled build behavior.
The same fix is applied to both
listiter_setstateandlistreviter_setstate,which share the same pattern.
__setstate__()differs for exhausted iterator GIL vs. non-GIL #129139