Skip to content

Fix RuntimeError when pretty-printing vars(posix) - #15463

Closed
Kavinraj065 wants to merge 2 commits into
ipython:mainfrom
Kavinraj065:fix-dict-pprint-mutation
Closed

Kavinraj065 wants to merge 2 commits into
ipython:mainfrom
Kavinraj065:fix-dict-pprint-mutation

Conversation

@Kavinraj065

@Kavinraj065 Kavinraj065 commented Nov 1, 2026 •

Copy link
Copy Markdown

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.

@Kavinraj065 Kavinraj065 changed the title Fix dict pprint mutation Fix RuntimeError when pretty-printing vars(posix) 🤖🤖 Nov 1, 2026
@Kavinraj065 Kavinraj065 changed the title Fix RuntimeError when pretty-printing vars(posix) 🤖🤖 Fix RuntimeError when pretty-printing vars(posix) Nov 1, 2026
@Carreau

Carreau commented Nov 1, 2026

Copy link
Copy Markdown
Member

Please check for existing PRs.

@Carreau Carreau closed this Nov 1, 2026
@Kavinraj065

Kavinraj065 commented Nov 1, 2026 via email •

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RuntimeError on first call to vars(posix)

2 participants