-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-145217: Add colour to pprint output
#145218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hugovk
wants to merge
11
commits into
python:main
Choose a base branch
from
hugovk:3.15-pprint-colour
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0906a8d
Add colour to pprint output
hugovk 71739fe
Add blurb
hugovk 1aac260
Merge remote-tracking branch 'upstream/main' into 3.15-pprint-colour
hugovk e11465b
Pass escape=False for pprint to keep real control chars
hugovk 7208828
Merge branch 'main' into 3.15-pprint-colour
hugovk fede255
Change back line endings to LF
hugovk 3597fbd
Fall back to monochrome if _colorize/_pyrepl.utils don't reify (import)
hugovk 0df1f64
Update NEWS
hugovk c3392e6
Avoid calling _format twice
hugovk 028ca36
Add test
hugovk fccaa12
Update test
hugovk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,18 +39,38 @@ | |
| import types as _types | ||
| from io import StringIO as _StringIO | ||
|
|
||
| lazy import _colorize | ||
| lazy from _pyrepl.utils import disp_str, gen_colors | ||
|
zooba marked this conversation as resolved.
|
||
|
|
||
| __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", | ||
| "PrettyPrinter", "pp"] | ||
|
|
||
|
|
||
| def pprint(object, stream=None, indent=1, width=80, depth=None, *, | ||
| compact=False, expand=False, sort_dicts=True, | ||
| underscore_numbers=False): | ||
| def pprint( | ||
| object, | ||
| stream=None, | ||
| indent=1, | ||
| width=80, | ||
| depth=None, | ||
| *, | ||
| color=True, | ||
| compact=False, | ||
| expand=False, | ||
| sort_dicts=True, | ||
| underscore_numbers=False, | ||
| ): | ||
| """Pretty-print a Python object to a stream [default is sys.stdout].""" | ||
| printer = PrettyPrinter( | ||
| stream=stream, indent=indent, width=width, depth=depth, | ||
| compact=compact, expand=expand, sort_dicts=sort_dicts, | ||
| underscore_numbers=underscore_numbers) | ||
| stream=stream, | ||
| indent=indent, | ||
| width=width, | ||
| depth=depth, | ||
| color=color, | ||
| compact=compact, | ||
| expand=expand, | ||
| sort_dicts=sort_dicts, | ||
| underscore_numbers=underscore_numbers, | ||
| ) | ||
| printer.pprint(object) | ||
|
|
||
|
|
||
|
|
@@ -111,10 +131,27 @@ def _safe_tuple(t): | |
| return _safe_key(t[0]), _safe_key(t[1]) | ||
|
|
||
|
|
||
| def _colorize_output(text): | ||
| """Apply syntax highlighting.""" | ||
| colors = list(gen_colors(text)) | ||
| chars, _ = disp_str(text, colors=colors, force_color=True, escape=False) | ||
| return "".join(chars) | ||
|
|
||
|
|
||
| class PrettyPrinter: | ||
| def __init__(self, indent=1, width=80, depth=None, stream=None, *, | ||
| compact=False, expand=False, sort_dicts=True, | ||
| underscore_numbers=False): | ||
| def __init__( | ||
| self, | ||
| indent=1, | ||
| width=80, | ||
| depth=None, | ||
| stream=None, | ||
| *, | ||
| color=True, | ||
| compact=False, | ||
| expand=False, | ||
| sort_dicts=True, | ||
| underscore_numbers=False, | ||
| ): | ||
| """Handle pretty printing operations onto a stream using a set of | ||
| configured parameters. | ||
|
|
||
|
|
@@ -131,6 +168,11 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *, | |
| The desired output stream. If omitted (or false), the standard | ||
| output stream available at construction will be used. | ||
|
|
||
| color | ||
| If true (the default), syntax highlighting is enabled for pprint | ||
| when the stream and environment variables permit. | ||
| If false, colored output is always disabled. | ||
|
|
||
| compact | ||
| If true, several items will be combined in one line. | ||
| Incompatible with expand mode. | ||
|
|
@@ -168,11 +210,30 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *, | |
| self._expand = bool(expand) | ||
| self._sort_dicts = sort_dicts | ||
| self._underscore_numbers = underscore_numbers | ||
| self._color = color | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we add a self._compact = bool(compact)
self._expand = bool(expand)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, can do, but not sure if it's necessary? |
||
|
|
||
| def pprint(self, object): | ||
| if self._stream is not None: | ||
| if self._stream is None: | ||
| return | ||
|
|
||
| use_color = False | ||
| if self._color: | ||
| try: | ||
| if _colorize.can_colorize(file=self._stream): | ||
| # Attempt to reify lazy imports, or ImportError | ||
| gen_colors, disp_str | ||
| use_color = True | ||
| except ImportError: | ||
| pass | ||
|
|
||
| if use_color: | ||
| sio = _StringIO() | ||
| self._format(object, sio, 0, 0, {}, 0) | ||
| self._stream.write(_colorize_output(sio.getvalue())) | ||
| else: | ||
| self._format(object, self._stream, 0, 0, {}, 0) | ||
| self._stream.write("\n") | ||
|
|
||
| self._stream.write("\n") | ||
|
|
||
| def pformat(self, object): | ||
| sio = _StringIO() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2026-02-25-16-19-21.gh-issue-145217.QQBY0-.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add color parameter to :func:`pprint.pprint` and :func:`pprint.pp`, enabling | ||
| syntax highlighting by default when the stream supports it. Patch by Hugo van | ||
| Kemenade. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.