mirror of
https://github.com/python/cpython.git
synced 2026-06-05 01:10:53 +00:00
gh-145378: Generate consistent colors for pdb commands (#149305)
This commit is contained in:
parent
a76d9573e4
commit
ddfdf9b8df
3 changed files with 45 additions and 0 deletions
21
Lib/pdb.py
21
Lib/pdb.py
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Generate consistent colors for :mod:`pdb` commands in :mod:`pdb` REPL.
|
||||
Loading…
Add table
Add a link
Reference in a new issue