Skip to content

Commit ce48cdd

Browse files
gaogaotiantianseehwan80
authored andcommitted
pythongh-131123: Support completion in pdb for convenience variable attributes (python#131124)
1 parent e77260b commit ce48cdd

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

Lib/pdb.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -975,24 +975,27 @@ def _complete_expression(self, text, line, begidx, endidx):
975975
# complete builtins, and they clutter the namespace quite heavily, so we
976976
# leave them out.
977977
ns = {**self.curframe.f_globals, **self.curframe.f_locals}
978-
if text.startswith("$"):
979-
# Complete convenience variables
980-
conv_vars = self.curframe.f_globals.get('__pdb_convenience_variables', {})
981-
return [f"${name}" for name in conv_vars if name.startswith(text[1:])]
982978
if '.' in text:
983979
# Walk an attribute chain up to the last part, similar to what
984980
# rlcompleter does. This will bail if any of the parts are not
985981
# simple attribute access, which is what we want.
986982
dotted = text.split('.')
987983
try:
988-
obj = ns[dotted[0]]
984+
if dotted[0].startswith('$'):
985+
obj = self.curframe.f_globals['__pdb_convenience_variables'][dotted[0][1:]]
986+
else:
987+
obj = ns[dotted[0]]
989988
for part in dotted[1:-1]:
990989
obj = getattr(obj, part)
991990
except (KeyError, AttributeError):
992991
return []
993992
prefix = '.'.join(dotted[:-1]) + '.'
994993
return [prefix + n for n in dir(obj) if n.startswith(dotted[-1])]
995994
else:
995+
if text.startswith("$"):
996+
# Complete convenience variables
997+
conv_vars = self.curframe.f_globals.get('__pdb_convenience_variables', {})
998+
return [f"${name}" for name in conv_vars if name.startswith(text[1:])]
996999
# Complete a simple name.
9971000
return [n for n in ns.keys() if n.startswith(text)]
9981001

Lib/test/test_pdb.py

+19
Original file line numberDiff line numberDiff line change
@@ -4478,6 +4478,25 @@ def test_builtin_completion(self):
44784478

44794479
self.assertIn(b'special', output)
44804480

4481+
def test_convvar_completion(self):
4482+
script = textwrap.dedent("""
4483+
import pdb; pdb.Pdb().set_trace()
4484+
""")
4485+
4486+
# Complete: $_frame
4487+
input = b"$_fram\t\n"
4488+
4489+
# Complete: $_frame.f_lineno + 100
4490+
input += b"$_frame.f_line\t + 100\n"
4491+
4492+
# Continue
4493+
input += b"c\n"
4494+
4495+
output = run_pty(script, input)
4496+
4497+
self.assertIn(b'<frame at 0x', output)
4498+
self.assertIn(b'102', output)
4499+
44814500
def test_local_namespace(self):
44824501
script = textwrap.dedent("""
44834502
def f():
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Supported completions for attributes of convenience variables in :mod:`pdb`.

0 commit comments

Comments
 (0)