Skip to content

gh-131123: Support completion in pdb for convenience variable attributes #131124

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 2 commits into from
Mar 12, 2025
Merged
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
13 changes: 8 additions & 5 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,24 +975,27 @@ def _complete_expression(self, text, line, begidx, endidx):
# complete builtins, and they clutter the namespace quite heavily, so we
# leave them out.
ns = {**self.curframe.f_globals, **self.curframe.f_locals}
if text.startswith("$"):
# Complete convenience variables
conv_vars = self.curframe.f_globals.get('__pdb_convenience_variables', {})
return [f"${name}" for name in conv_vars if name.startswith(text[1:])]
if '.' in text:
# Walk an attribute chain up to the last part, similar to what
# rlcompleter does. This will bail if any of the parts are not
# simple attribute access, which is what we want.
dotted = text.split('.')
try:
obj = ns[dotted[0]]
if dotted[0].startswith('$'):
obj = self.curframe.f_globals['__pdb_convenience_variables'][dotted[0][1:]]
else:
obj = ns[dotted[0]]
for part in dotted[1:-1]:
obj = getattr(obj, part)
except (KeyError, AttributeError):
return []
prefix = '.'.join(dotted[:-1]) + '.'
return [prefix + n for n in dir(obj) if n.startswith(dotted[-1])]
else:
if text.startswith("$"):
# Complete convenience variables
conv_vars = self.curframe.f_globals.get('__pdb_convenience_variables', {})
Copy link
Member

Choose a reason for hiding this comment

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

Here you use get and in line 985 you don't. Is that correct?

Copy link
Member Author

@gaogaotiantian gaogaotiantian Mar 12, 2025

Choose a reason for hiding this comment

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

line 985 is in a try block which has a KeyError catch. If the first part starts with $ and does not exist in globals, we should just return []. That's also how it handles the other parts.

return [f"${name}" for name in conv_vars if name.startswith(text[1:])]
# Complete a simple name.
return [n for n in ns.keys() if n.startswith(text)]

Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4478,6 +4478,25 @@ def test_builtin_completion(self):

self.assertIn(b'special', output)

def test_convvar_completion(self):
script = textwrap.dedent("""
import pdb; pdb.Pdb().set_trace()
""")

# Complete: $_frame
input = b"$_fram\t\n"

# Complete: $_frame.f_lineno + 100
input += b"$_frame.f_line\t + 100\n"

# Continue
input += b"c\n"

output = run_pty(script, input)

self.assertIn(b'<frame at 0x', output)
self.assertIn(b'102', output)

def test_local_namespace(self):
script = textwrap.dedent("""
def f():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Supported completions for attributes of convenience variables in :mod:`pdb`.
Loading