Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Enable auto-indent for pdb's multi-line mode
  • Loading branch information
gaogaotiantian committed May 3, 2025
commit 71597b31edc035af3aea0b52e48c7769a064d41c
32 changes: 29 additions & 3 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,12 +743,34 @@ def displayhook(self, obj):
self.message(repr(obj))

@contextmanager
def _enable_multiline_completion(self):
def _enable_multiline_input(self):
try:
import readline
except ImportError:
yield
return

def input_auto_indent():
last_index = readline.get_current_history_length()
last_line = readline.get_history_item(last_index)
if last_line:
if last_line.isspace():
# If the last line is empty, we don't need to indent
return

last_line = last_line.rstrip('\r\n')
indent = len(last_line) - len(last_line.lstrip())
if last_line.endswith(":"):
indent += 4
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.

Is indent always 4?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We kind of dictated there, it is consistent with how we auto-fill the space when we hit <tab>. However, we can be smart and search for the history for the last indent. I have the time to do it and it should not be rocket science. Do you want me to do that?

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.

Up to you. This is fine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Let's land this today and if people are complaining, we can treat that as a bug and fix it later :)

readline.insert_text(' ' * indent)

completenames = self.completenames
try:
self.completenames = self.complete_multiline_names
readline.set_startup_hook(input_auto_indent)
yield
finally:
readline.set_startup_hook()
self.completenames = completenames
return

Expand Down Expand Up @@ -857,7 +879,7 @@ def _read_code(self, line):
try:
if (code := codeop.compile_command(line + '\n', '<stdin>', 'single')) is None:
# Multi-line mode
with self._enable_multiline_completion():
with self._enable_multiline_input():
buffer = line
continue_prompt = "... "
while (code := codeop.compile_command(buffer, '<stdin>', 'single')) is None:
Expand All @@ -879,7 +901,11 @@ def _read_code(self, line):
return None, None, False
else:
line = line.rstrip('\r\n')
buffer += '\n' + line
if line.isspace():
# empty line, just continue
buffer += '\n'
else:
buffer += '\n' + line
self.lastcmd = buffer
except SyntaxError as e:
# Maybe it's an await expression/statement
Expand Down
40 changes: 31 additions & 9 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4812,14 +4812,35 @@ def f():

self.assertIn(b'I love Python', output)

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

input = b"def f(x):\n"
input += b"if x > 0:\n"
input += b"x += 1\n"
input += b"return x\n"
# We need to do backspaces to remove the auto-indentation
input += b"\x08\x08\x08\x08else:\n"
input += b"return -x\n"
input += b"\n"
input += b"f(-21-21)\n"
input += b"c\n"

output = run_pty(script, input)

self.assertIn(b'42', output)

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

input = b"def func():\n"
# Complete: \treturn 40 + 2
input += b"\tret\t 40 + 2\n"
# Auto-indent
# Complete: return 40 + 2
input += b"ret\t 40 + 2\n"
input += b"\n"
# Complete: func()
input += b"fun\t()\n"
Expand All @@ -4839,20 +4860,21 @@ def test_multiline_indent_completion(self):
# if the completion is not working as expected
input = textwrap.dedent("""\
def func():
\ta = 1
\ta += 1
\ta += 1
\tif a > 0:
a += 1
\t\treturn a
a = 1
\x08\ta += 1
\x08\x08\ta += 1
\x08\x08\x08\ta += 1
\x08\x08\x08\x08\tif a > 0:
a += 1
\x08\x08\x08\x08return a

func()
c
""").encode()

output = run_pty(script, input)

self.assertIn(b'4', output)
self.assertIn(b'5', output)
self.assertNotIn(b'Error', output)

def test_interact_completion(self):
Expand Down
Loading