Skip to content

gh-133351: Fix remote PDB's multi-line block tab completion #133387

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
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: 10 additions & 3 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2933,6 +2933,7 @@ def __init__(self, pid, sockfile, interrupt_script):
self.completion_matches = []
self.state = "dumb"
self.write_failed = False
self.multiline_block = False

def _ensure_valid_message(self, msg):
# Ensure the message conforms to our protocol.
Expand Down Expand Up @@ -2979,6 +2980,7 @@ def _send(self, **kwargs):
self.write_failed = True

def read_command(self, prompt):
self.multiline_block = False
reply = input(prompt)

if self.state == "dumb":
Expand All @@ -3003,6 +3005,7 @@ def read_command(self, prompt):
return prefix + reply

# Otherwise, valid first line of a multi-line statement
self.multiline_block = True
continue_prompt = "...".ljust(len(prompt))
while codeop.compile_command(reply, "<stdin>", "single") is None:
reply += "\n" + input(continue_prompt)
Expand Down Expand Up @@ -3105,9 +3108,13 @@ def complete(self, text, state):

origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if self.multiline_block:
# We're completing a line contained in a multi-line block.
# Force the remote to treat it as a Python expression.
line = "! " + line
offset = len(origline) - len(line)
begidx = readline.get_begidx() - offset
endidx = readline.get_endidx() - offset

msg = {
"complete": {
Expand Down
38 changes: 38 additions & 0 deletions Lib/test/test_remote_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,44 @@ def test_completion_in_pdb_state(self):
expected_state={"state": "pdb"},
)

def test_multiline_completion_in_pdb_state(self):
"""Test requesting tab completions at a (Pdb) continuation prompt."""
# GIVEN
incoming = [
Comment on lines +535 to +537
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"""Test requesting tab completions at a (Pdb) continuation prompt."""
# GIVEN
incoming = [
"""Test requesting tab completions at a (Pdb) continuation prompt."""
incoming = [

Copy link
Member

Choose a reason for hiding this comment

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

I kind of agree that #GIVEN is unnecessary here. However, that's consistent with other tests and it's not super important at this point. I do think in the future we should have a cleanup PR to clean these out. It's kind of redundent at this point.

("server", {"prompt": "(Pdb) ", "state": "pdb"}),
("user", {"prompt": "(Pdb) ", "input": "if True:"}),
(
"user",
{
"prompt": "... ",
"completion_request": {
"line": " b",
"begidx": 4,
"endidx": 5,
},
"input": " bool()",
},
),
("server", {"completions": ["bin", "bool", "bytes"]}),
("user", {"prompt": "... ", "input": ""}),
]
self.do_test(
incoming=incoming,
expected_outgoing=[
{
"complete": {
"text": "b",
"line": "! b",
"begidx": 2,
"endidx": 3,
}
},
{"reply": "if True:\n bool()\n"},
],
expected_completions=["bin", "bool", "bytes"],
expected_state={"state": "pdb"},
)

def test_completion_in_interact_state(self):
"""Test requesting tab completions at a >>> prompt."""
incoming = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix remote PDB to correctly request tab completions for Python expressions
from the server when completing a continuation line of a multi-line Python
block.
Loading