Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -463,6 +464,26 @@ def complete(self, text, state):
except IndexError:
return None

def gen_colors(self, buffer):
from _pyrepl.utils import ColorSpan, Span
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just nit comment, now we can use lazy import right?


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
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generate consistent colors for :mod:`pdb` commands in :mod:`pdb` REPL.
Loading