Uh oh!
There was an error while loading. Please reload this page.
gh-145217: Add colour to pprint output - #145218
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Uh oh!
There was an error while loading. Please reload this page.
| self._expand = bool(expand) | ||
| self._sort_dicts = sort_dicts | ||
| self._underscore_numbers = underscore_numbers | ||
| self._color = color |
There was a problem hiding this comment.
Shall we add a bool(color) conversion just as a few lines above?
self._compact=bool(compact)
self._expand=bool(expand)There was a problem hiding this comment.
Sure, can do, but not sure if it's necessary?
There was a problem hiding this comment.
Do we have explicit test for color=True and can_colorize=False? It's an end to end test.
something like
obj= {"key": "value"}
stream=io.StringIO()
# color=True should not produce no ANSI codes for streams# that do not support colorpprint.pprint(obj, stream=stream, color=True)
result=stream.getvalue()
self.assertNotIn("\x1b[", result)Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Uh oh!
There was an error while loading. Please reload this page.
sunmy2019
commented
Apr 28, 2026
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
hugovk
commented
Apr 28, 2026
Documentation build overview
105 files changed · |
I agree with this. But thinking further, our current highlighting logic relies on So, instead of special-casing Probe the text with tokenize first, No blacklist needed, no half-highlighted output. What do you think? def _colorize_output(text):
"""Apply syntax highlighting."""
- if "\x1b[" in text:- # If the text already contains ANSI escape sequences- # (for example, from a custom __repr__),- # return as-is to avoid breaking their color.+ # Probe: if the text cannot be fully tokenized, fall back to plain text+ # to avoid the "half-highlighted" problem.+ try:+ list(tokenize.generate_tokens(_StringIO(text).readline))+ except (SyntaxError, tokenize.TokenError):
return text
colors = list(gen_colors(text))
chars, _ = disp_str(text, colors=colors, force_color=True, escape=False)
|
hugovk
commented
May 3, 2026
Could do, although that'd mean we're potentially doing |
sunmy2019
commented
May 3, 2026
If that matters, we could modify the latter to raise errors (rather than catch them) |
hugovk
commented
May 4, 2026
I think I'll leave it like this, unless others also want to suggest something. We can always fix it later if needed. Thanks for the review! |
encukou
commented
May 5, 2026
hugovk
commented
May 5, 2026
Updated to skip those. |
encukou
commented
May 5, 2026
hugovk
commented
May 6, 2026
Hmm, the root issue is that I'm personally fine with the current version or the previous, but also fine in skipping this for 3.15 and doing it properly for 3.16 (or just closing :) |
encukou
commented
May 6, 2026
Yeah. That guess might not be right; let's resist the temptation. In 3.16 a pprint refactor might be needed anyway, for PEP-813. |


Example:
pprintoutput #145217📚 Documentation preview 📚: https://cpython-previews--145218.org.readthedocs.build/