Repository navigation
Fix RuntimeError when pretty-printing vars(posix) - #15463
Closed
Kavinraj065 wants to merge 2 commits into
Closed
Kavinraj065 wants to merge 2 commits into
Kavinraj065 wants to merge 2 commits into
Conversation
Member
|
Please check for existing PRs. |
Author
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.
Fixes #14541.
Pretty-printing a dict crashes with "dictionary changed size during iteration" if printing one of its values adds a key to that same dict. That's what happens with vars(posix) on Linux: printing posix.memfd_create calls inspect.signature(), which resolves the default flags=MFD_CLOEXEC with eval('MFD_CLOEXEC', posix.dict). eval adds builtins to that dict, so it grows while the dict printer is iterating over obj.keys(). After the first call builtins is already there, which is why only the first call fails. [*vars(posix).items()] works because unpacking never calls inspect.signature().
The fix iterates over a copy of the keys, like the set printer already does with a sorted copy.
The first commit adds two tests that fail without the fix: one for the actual vars(posix) case on Linux, and one that reproduces the same thing (eval adding builtins to the dict being printed) on any platform.
References
#15458 makes the same one-line change. This PR adds the root-cause explanation and a regression test for the vars(posix) case itself.
AI usage 🤖🤖
Claude Code in VS Code (model: OPUS 5.5, effort level: HIGH) and Claude Opus 5.5 on claude.ai (effort level unknown) were used to trace the root cause and write the tests and fix. I directed the work, read the whole diff and ran the change.