Skip to content

gh-145217: Add colour to pprint output#145218

Open
hugovk wants to merge 4 commits intopython:mainfrom
hugovk:3.15-pprint-colour
Open

gh-145217: Add colour to pprint output#145218
hugovk wants to merge 4 commits intopython:mainfrom
hugovk:3.15-pprint-colour

Conversation

@hugovk
Copy link
Copy Markdown
Member

@hugovk hugovk commented Feb 25, 2026

Example:

import pprint

data = ["spam", "eggs", ("lumberjack", "knights"), "ni", 42, None]
data.insert(0, data)

pprint.pp(data)
pprint.pprint(data)
pprint.PrettyPrinter().pprint(data)
image

📚 Documentation preview 📚: https://cpython-previews--145218.org.readthedocs.build/

@hugovk hugovk requested a review from AA-Turner as a code owner February 25, 2026 14:20
@hugovk hugovk added type-feature A feature request or enhancement stdlib Standard Library Python modules in the Lib/ directory labels Feb 25, 2026
@StanFromIreland

This comment was marked as resolved.

from io import StringIO as _StringIO

lazy import _colorize
lazy from _pyrepl.utils import disp_str, gen_colors
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Steve won't be happy: #145035

(We could move it, #142699)

@StanFromIreland
Copy link
Copy Markdown
Member

Something is converting the newlines to ^Js:

>>> import pprint
... 
... data = ["spam", "eggs", ("lumberjack", "knights"), "ni", 42, None]
... data.insert(0, data)
... 
... pprint.pp(data)
... pprint.pprint(data)
... pprint.PrettyPrinter().pprint(data)
... 
[<Recursion on list with id=140532389171664>,^J 'spam',^J 'eggs',^J ('lumberjack', 'knights'),^J 'ni',^J 42,^J None]
[<Recursion on list with id=140532389171664>,^J 'spam',^J 'eggs',^J ('lumberjack', 'knights'),^J 'ni',^J 42,^J None]
[<Recursion on list with id=140532389171664>,^J 'spam',^J 'eggs',^J ('lumberjack', 'knights'),^J 'ni',^J 42,^J None]

@hugovk
Copy link
Copy Markdown
Member Author

hugovk commented Apr 9, 2026

Looks like this is a change introduced by #146584 that just got merged in from main, pyrepl uses caret versions of control characters like ^J for \n.

Updated to pass a flag to avoid this escaping for pprint:

>>> import pprint
...
... data = ["spam", "eggs", ("lumberjack", "knights"), "ni", 42, None]
... data.insert(0, data)
...
... pprint.pp(data)
... pprint.pprint(data)
... pprint.PrettyPrinter().pprint(data)
...
[<Recursion on list with id=4395579856>,
 'spam',
 'eggs',
 ('lumberjack', 'knights'),
 'ni',
 42,
 None]
[<Recursion on list with id=4395579856>,
 'spam',
 'eggs',
 ('lumberjack', 'knights'),
 'ni',
 42,
 None]
[<Recursion on list with id=4395579856>,
 'spam',
 'eggs',
 ('lumberjack', 'knights'),
image

@johnslavik johnslavik self-requested a review April 10, 2026 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review stdlib Standard Library Python modules in the Lib/ directory type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants