gh-158254: Detect keys changing during reverse dict iteration - #158255
Merged
Merged
Conversation
A reverse dict iterator only checked that the dict's size was unchanged, so after the keys were replaced with the same number of keys it kept yielding entries for the new keys. It could yield more items than the dict had when it was created, and __length_hint__() then wrapped around. Add the check the forward iterators already have: finding an entry after the expected number of items raises "dictionary keys changed during iteration". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks @jab for the PR, and @ZeroIntensity for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15. |
|
GH-158267 is a backport of this pull request to the 3.15 branch. |
|
GH-158268 is a backport of this pull request to the 3.14 branch. |
|
GH-158269 is a backport of this pull request to the 3.13 branch. |
methane
approved these changes
Sep 27, 2026
ZeroIntensity
pushed a commit
that referenced
this pull request
Sep 27, 2026
…H-158255) (GH-158269) A reverse dict iterator only checked that the dict's size was unchanged, so after the keys were replaced with the same number of keys it kept yielding entries for the new keys. It could yield more items than the dict had when it was created, and __length_hint__() then wrapped around. Add the check the forward iterators already have: finding an entry after the expected number of items raises "dictionary keys changed during iteration". (cherry picked from commit 042a7c1) Co-authored-by: Joshua Bronson <jabronson@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
ZeroIntensity
pushed a commit
that referenced
this pull request
Sep 27, 2026
…H-158255) (GH-158268) A reverse dict iterator only checked that the dict's size was unchanged, so after the keys were replaced with the same number of keys it kept yielding entries for the new keys. It could yield more items than the dict had when it was created, and __length_hint__() then wrapped around. Add the check the forward iterators already have: finding an entry after the expected number of items raises "dictionary keys changed during iteration". (cherry picked from commit 042a7c1) Co-authored-by: Joshua Bronson <jabronson@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.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.
Reverse dict iterators only checked that the dict's size was unchanged. So after
clear()and a refill to the same size that leaves live entries below the iterator's position, they kept yielding entries for the new keys. They could yield more items than the dict had when the iterator was created, and__length_hint__()then wrapped around to 2**64-1.This adds the check the forward iterators already have (e.g.
dictiter_iternextkey_lock_held): finding an entry after the expected number of items raisesRuntimeError: dictionary keys changed during iteration.The new test fails without the change ("RuntimeError not raised") and passes with it. The full test suite passes on a macOS arm64 debug build (not free-threaded).
This change was written with the help of Claude Code, an AI assistant. I have reviewed it.
🤖 Generated with Claude Code