Repository navigation
Fix RuntimeError when pretty-printing a dict that changes while print… - #15458
omsingh265 wants to merge 3 commits into
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
…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
…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