Skip to content

Fix dictionary mutation during pretty printing - #15474

Open
Parinika wants to merge 2 commits into
ipython:mainfrom
Parinika:fix/dict-pretty-print-mutation
Open

Parinika wants to merge 2 commits into
ipython:mainfrom
Parinika:fix/dict-pretty-print-mutation

Conversation

@Parinika

@Parinika Parinika commented Nov 6, 2026

Copy link
Copy Markdown

Summary

Fixes a RuntimeError: dictionary changed size during iteration that can occur when IPython pretty-prints dictionaries whose contents change during formatting.

Problem

IPython's dictionary pretty-printer iterates over dictionary keys while formatting their associated values. In certain cases, formatting can trigger changes to the dictionary, invalidating the active iterator.

This can be reproduced using:

import posix
vars(posix)

Solution

Updated the dictionary pretty-printer in IPython/lib/pretty.py to iterate over a snapshot of dictionary keys rather than a live dictionary view.

This prevents changes to dictionary size from invalidating the key iterator during formatting.

Tests

Added regression tests covering:

  • Dictionary mutation during pretty-printing.
  • Standard dictionary formatting.
  • Recursive dictionary formatting.
  • Pretty-printing the posix module dictionary.

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.

1 participant