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
Rename REPL to Syntax now that pdb is using it, too; adapt pdb tests
  • Loading branch information
ambv committed May 5, 2025
commit e67fda9862b53bf1ea3d2580233f3d0ae13119af
12 changes: 6 additions & 6 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ANSIColors:
# - Create a theme by copying an existing `Theme` with one or more sections
# replaced, using `default_theme.copy_with()`;
# - create a theme section by copying an existing `ThemeSection` with one or
# more colors replaced, using for example `default_theme.repl.copy_with()`;
# more colors replaced, using for example `default_theme.syntax.copy_with()`;
# - create a theme from scratch by instantiating a `Theme` data class with
# the required sections (which are also dataclass instances).
#
Expand Down Expand Up @@ -153,7 +153,7 @@ class Argparse(ThemeSection):


@dataclass(frozen=True)
class REPL(ThemeSection):
class Syntax(ThemeSection):
prompt: str = ANSIColors.BOLD_MAGENTA
keyword: str = ANSIColors.BOLD_BLUE
builtin: str = ANSIColors.CYAN
Expand Down Expand Up @@ -195,15 +195,15 @@ class Theme:
below.
"""
argparse: Argparse = field(default_factory=Argparse)
repl: REPL = field(default_factory=REPL)
syntax: Syntax = field(default_factory=Syntax)
traceback: Traceback = field(default_factory=Traceback)
unittest: Unittest = field(default_factory=Unittest)

def copy_with(
self,
*,
argparse: Argparse | None = None,
repl: REPL | None = None,
syntax: Syntax | None = None,
traceback: Traceback | None = None,
unittest: Unittest | None = None,
) -> Self:
Expand All @@ -214,7 +214,7 @@ def copy_with(
"""
return type(self)(
argparse=argparse or self.argparse,
repl=repl or self.repl,
syntax=syntax or self.syntax,
traceback=traceback or self.traceback,
unittest=unittest or self.unittest,
)
Expand All @@ -228,7 +228,7 @@ def no_colors(self) -> Self:
"""
return type(self)(
argparse=self.argparse.no_colors(),
repl=self.repl.no_colors(),
syntax=self.syntax.no_colors(),
traceback=self.traceback.no_colors(),
unittest=self.unittest.no_colors(),
)
Expand Down
2 changes: 1 addition & 1 deletion Lib/_pyrepl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

def THEME():
# Not cached: the user can modify the theme inside the interactive session.
return _colorize.get_theme().repl
return _colorize.get_theme().syntax


class Span(NamedTuple):
Expand Down
2 changes: 1 addition & 1 deletion Lib/asyncio/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def run(self):

ps1 = getattr(sys, "ps1", ">>> ")
if CAN_USE_PYREPL:
theme = get_theme().repl
theme = get_theme().syntax
ps1 = f"{theme.prompt}{ps1}{theme.reset}"
console.write(f"{ps1}import asyncio\n")

Expand Down
2 changes: 1 addition & 1 deletion Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
self._wait_for_mainpyfile = False
self.tb_lineno = {}
self.mode = mode
self.colorize = _colorize.can_colorize(file=stdout or sys.stdout) and colorize
self.colorize = colorize and _colorize.can_colorize(file=stdout or sys.stdout)
# Try to load readline if it exists
try:
import readline
Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from contextlib import ExitStack, redirect_stdout
from io import StringIO
from test import support
from test.support import force_not_colorized, has_socket_support, os_helper
from test.support import has_socket_support, os_helper
from test.support.import_helper import import_module
from test.support.pty_helper import run_pty, FakeInput
from test.support.script_helper import kill_python
Expand Down Expand Up @@ -3743,7 +3743,6 @@ def start_pdb():
self.assertNotIn(b'Error', stdout,
"Got an error running test script under PDB")

@force_not_colorized
def test_issue16180(self):
# A syntax error in the debuggee.
script = "def f: pass\n"
Expand All @@ -3757,7 +3756,6 @@ def test_issue16180(self):
'Fail to handle a syntax error in the debuggee.'
.format(expected, stderr))

@force_not_colorized
def test_issue84583(self):
# A syntax error from ast.literal_eval should not make pdb exit.
script = "import ast; ast.literal_eval('')\n"
Expand Down Expand Up @@ -4691,7 +4689,7 @@ def foo():
self.assertIn("42", stdout)


@unittest.skipUnless(_colorize.can_colorize(), "Test requires colorize")
@support.force_colorized_test_class
class PdbTestColorize(unittest.TestCase):
def setUp(self):
self._original_can_colorize = _colorize.can_colorize
Expand Down Expand Up @@ -4748,6 +4746,7 @@ def test_return_from_inline_mode_to_REPL(self):
self.assertEqual(p.returncode, 0)


@support.force_not_colorized_test_class
@support.requires_subprocess()
class PdbTestReadline(unittest.TestCase):
def setUpClass():
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pyrepl/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


overrides = {"reset": "z", "soft_keyword": "K"}
colors = {overrides.get(k, k[0].lower()): v for k, v in default_theme.repl.items()}
colors = {overrides.get(k, k[0].lower()): v for k, v in default_theme.syntax.items()}


@force_not_colorized_test_class
Expand Down
Loading
0