| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
| 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)
Sorry, something went wrong.
There was a problem hiding this comment.
Sure, can do, but not sure if it's necessary?
Sorry, something went wrong.
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 color
pprint.pprint(obj, stream=stream, color=True)
result = stream.getvalue()
self.assertNotIn("\x1b[", result)
Sorry, something went wrong.
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Sorry, something went wrong.
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Sorry, something went wrong.
Documentation build overview105 files changed · + 2 added · ± 103 modified + Added ± Modified |
Sorry, something went wrong.
I agree with this. But thinking further, our current highlighting logic relies on tokenize, yet pprint output has never made any guarantee that it must be valid Python source. __repr__ can return anything — it doesn't have to be tokenizer-friendly. So, instead of special-casing \x1b[, how about 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)
|
Sorry, something went wrong.
|
Could do, although that'd mean we're potentially doing tokenize.generate_tokens() both here and in gen_colors(). Maybe that doesn't matter. |
Sorry, something went wrong.
If that matters, we could modify the latter to raise errors (rather than catch them) |
Sorry, something went wrong.
|
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! |
Sorry, something went wrong.
|
Updated to skip those. |
Sorry, something went wrong.
|
Hmm, the root issue is that gen_colors tokenises as if it were Python source, so I think it would quite a big refactor to do that, at least to make it into 3.15. 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 :) |
Sorry, something went wrong.
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. |
Sorry, something went wrong.
|
This PR is stale because it has been open for 90 days with no activity. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Example:
📚 Documentation preview 📚: https://cpython-previews--145218.org.readthedocs.build/