Skip to content

Fix RuntimeError when pretty-printing a dict that changes while print… - #15458

Open
omsingh265 wants to merge 3 commits into
ipython:mainfrom
omsingh265:fix-pretty-dict-mutation-14541
Open

omsingh265 wants to merge 3 commits into
ipython:mainfrom
omsingh265:fix-pretty-dict-mutation-14541

Conversation

@omsingh265

Copy link
Copy Markdown

…ingIterate over a snapshot of the keys, so values that add entries to thedict they belong to no longer break the printer. Fixes theRuntimeError on the first vars(posix) call on Linux.Add a test that mutates a dict from inside a value's repr_pretty.Fixes #14541

…ingIterate over a snapshot of the keys, so values that add entries to thedict they belong to no longer break the printer. Fixes theRuntimeError on the first vars(posix) call on Linux.Add a test that mutates a dict from inside a value's _repr_pretty_.Fixes ipython#14541

@Marwan-verse Marwan-verse left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the key snapshot respect max_seq_length? pretty() defaults to displaying at most 1,000 entries, but list(obj.keys()) copies every key before that limit is applied. With a million-entry dictionary and max_seq_length=2, I measured about 8 MB of peak traced allocations in the PR's pretty.py, versus about 11 KB with the original obj.keys() expression; both display just two entries and an ellipsis. A bounded snapshot could avoid making memory and time scale with the full dictionary when only a small prefix is displayed.

Prepared with Codex assistance. This measurement used the exact PR-head pretty.py in a local Python runtime; the comparison replaced the changed expression with its base version. It was not a full checkout or container test.

omsingh265 and others added 2 commits November 6, 2026 12:03
…ingSnapshot at most max_seq_length + 1 keys before iterating, so valuesthat add entries to their own dict (e.g. vars(posix) on Linux) nolonger break the printer

This branch has not been deployed

No deployments
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