8000 gh-133346: Make theming support in _colorize extensible by ambv · Pull Request #133347 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-133346: Make theming support in _colorize extensible #133347

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

Merged
merged 21 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix WASI test failures
  • Loading branch information
ambv committed May 3, 2025
commit b29c9b9ab4909e14f5d87bdc577da9f573466ba3
9 changes: 7 additions & 2 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,13 @@ def can_colorize(*, file: IO[str] | IO[bytes] | None = None) -> bool:
theme_no_color = default_theme.no_colors()


def get_theme(*, tty_file: IO[str] | IO[bytes] | None = None) -> Theme:
if can_colorize(file=tty_file):
def get_theme(
*,
tty_file: IO[str] | IO[bytes] | None = None,
force_color: bool = False,
force_no_color: bool = False,
) -> Theme:
if force_color or (not force_no_color and can_colorize(file=tty_file)):
return _theme
return theme_no_color

Expand Down
6 changes: 1 addition & 5 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,7 @@ def get_prompt(self, lineno: int, cursor_on_line: bool) -> str:

if self.can_colorize:
t = THEME()
prompt = (
f"{t.prompt}"
f"{prompt}"
f"{t.reset}"
)
prompt = f"{t.prompt}{prompt}{t.reset}"
return prompt

def push_input_trans(self, itrans: input.KeymapTranslator) -> None:
Expand Down
17 changes: 12 additions & 5 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import _colorize

from contextlib import suppress
from _colorize import get_theme, theme_no_color

__all__ = ['extract_stack', 'extract_tb', 'format_exception',
'format_exception_only', 'format_list', 'format_stack',
Expand Down Expand Up @@ -187,7 +186,10 @@ def format_exception_only(exc, /, value=_sentinel, *, show_group=False, **kwargs
def _format_final_exc_line(etype, value, *, insert_final_newline=True, colorize=False):
valuestr = _safe_string(value, 'exception')
end_char = "\n" if insert_final_newline else ""
theme = (theme_no_color if not colorize else get_theme()).traceback
if colorize:
theme = _colorize.get_theme(force_color=True).traceback
else:
theme = _colorize.get_theme(force_no_color=True).traceback
if value is None or not valuestr:
line = f"{theme.type}{etype}{theme.reset}{end_char}"
else:
Expand Down Expand Up @@ -534,8 +536,10 @@ def format_frame_summary(self, frame_summary, **kwargs):
filename = frame_summary.filename
if frame_summary.filename.startswith("<stdin>-"):
filename = "<stdin>"

theme = (theme_no_color if not colorize else get_theme()).traceback
if colorize:
theme = _colorize.get_theme(force_color=True).traceback
else:
theme = _colorize.get_theme(force_no_color=True).traceback
row.append(
' File {}"{}"{}, line {}{}{}, in {}{}{}\n'.format(
theme.filename,
Expand Down Expand Up @@ -1373,7 +1377,10 @@ def _format_syntax_error(self, stype, **kwargs):
"""Format SyntaxError exceptions (internal helper)."""
# Show exactly where the problem was found.
colorize = kwargs.get("colorize", False)
theme = (theme_no_color if not colorize else get_theme()).traceback
if colorize:
theme = _colorize.get_theme(force_color=True).traceback
else:
theme = _colorize.get_theme(force_no_color=True).traceback
filename_suffix = ''
if self.lineno is not None:
yield ' File {}"{}"{}, line {}{}{}\n'.format(
Expand Down
Loading
0