GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-161114: Speed up `pprint` on deeply nested containers by gpshead · Pull Request #161115 · python/cpython

Conversation

@gpshead

@gpshead gpshead commented Nov 1, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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

  • Issue: Speed up pprint on deeply nested containers #161114

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gpshead
pythongh-161114: Speed up pprint on deeply nested containers
cb92f79
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.
@gpshead gpshead self-assigned this Nov 1, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 1, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Nov 1, 2026
Speed up pprint on deeply nested containers #161114
Open
@gpshead
gpshead marked this pull request as ready for review November 1, 2026 18:50
@bedevere-app bedevere-app Bot added the awaiting core review label Nov 1, 2026
@gpshead
gpshead requested a review from warsaw November 1, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@warsaw warsaw Awaiting requested review from warsaw

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@gpshead gpshead

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Speed up pprint on deeply nested containers

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@gpshead
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.