gh-145378: Generate consistent colors for pdb commands (#149305)

This commit is contained in:
Tian Gao 2026-05-05 01:25:45 -07:00 committed by GitHub
parent a76d9573e4
commit ddfdf9b8df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 0 deletions

View file

@ -402,6 +402,7 @@ def __init__(self, pdb_instance, stdin, stdout, prompt):
completer_delims=frozenset(' \t\n`@#%^&*()=+[{]}\\|;:\'",<>?')
)
)
self.readline_wrapper.get_reader().gen_colors = self.gen_colors
def readline(self):
@ -463,6 +464,26 @@ def complete(self, text, state):
except IndexError:
return None
def gen_colors(self, buffer):
from _pyrepl.utils import ColorSpan, Span
if not buffer.strip():
return
leading_spaces = len(buffer) - len(buffer.lstrip())
leading_text = buffer.split()[0]
if hasattr(self.pdb_instance, 'do_' + leading_text):
yield ColorSpan(
Span(leading_spaces, leading_spaces + len(leading_text) - 1),
"soft_keyword"
)
# Redact the command text with spaces so there will be no duplicated
# color span generated for it later.
redact_length = leading_spaces + len(leading_text)
buffer = ' ' * redact_length + buffer[redact_length:]
yield from _pyrepl.utils.gen_colors(buffer)
class Pdb(bdb.Bdb, cmd.Cmd):
_previous_sigint_handler = None

View file

@ -4981,6 +4981,29 @@ def test_stack_entry(self):
p.set_trace(commands=['w', 'c'])
self.assertIn("\x1b", output.getvalue())
@unittest.skipIf(not pdb._pyrepl_available(), "pyrepl is not available")
def test_gen_colors(self):
p = pdb.Pdb()
gen_colors = p.pyrepl_input.gen_colors
test_cases = [
("longlist", [((0, 7), "soft_keyword")]),
("!longlist", [((0, 0), "op")]),
("list", [((0, 3), "soft_keyword")]),
("list(", [((0, 3), "builtin"), ((4, 4), "op")]),
("a = 1", [
((0, 0), "soft_keyword"),
((2, 2), "op"),
((4, 4), "number"),
])
]
for buffer, expected in test_cases:
for color_span, ((start, end), tag) in zip(gen_colors(buffer), expected, strict=True):
self.assertEqual(color_span.span.start, start)
self.assertEqual(color_span.span.end, end)
self.assertEqual(color_span.tag, tag)
@support.force_not_colorized_test_class
@support.requires_subprocess()

View file

@ -0,0 +1 @@
Generate consistent colors for :mod:`pdb` commands in :mod:`pdb` REPL.