GeezerVison is showing github.com — gh-161114: Speed up `pprint` on deeply nested containers by gpshead · Pull Request #161115 · python/cpython
Conversation
gpshead
commented
Nov 1, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
PrettyPrinter._format() now asks _safe_repr() for a container's one-line repr with the remaining line width as a limit, and the list/tuple, dict and dict-view loops stop with a private _TooWide as soon as the repr gets wider than that, so the "does it fit?" check costs at most a line's worth of work per nesting level instead of the whole subtree. The limit is passed through an instance attribute so format()'s signature is unchanged, and it is not applied when format() is overridden, since an override may shorten what it gets from PrettyPrinter.format(); such subclasses keep the current speed. pformat() output, isreadable() and isrecursive() were compared against main on 18,000 random nested structures with various width/compact/expand/depth/sort_dicts options and are identical. A 300-deep list of 20,000 ints goes from 2.7 s to 0.11 s and ordinary wrapped output gets about 20% faster; small objects that fit on one line are a few percent slower from the extra bookkeeping.
Fixes #161114
ANT-2026-1RHKXQEC
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Stop building a container's one-line repr as soon as it is wider than the
remaining line, instead of formatting the whole subtree at every level.
Reviewers
warsaw
Awaiting requested review from warsaw
Uh oh!
There was an error while loading. Please reload this page.
Milestone
No milestone
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.