Skip to content

gh-125377: Improve tab indentation for pdb multi-line input #130471

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 6 commits into from
Mar 4, 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
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,10 @@ pdb
(if any).
(Contributed by Tian Gao in :gh:`130493`.)

* ``<tab>`` at the beginning of the line in :mod:`pdb` multi-line input will
fill in a 4-space indentation now, instead of inserting a ``\t`` character.
(Contributed by Tian Gao in :gh:`130471`.)


pickle
------
Expand Down
21 changes: 18 additions & 3 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,10 @@ def displayhook(self, obj):
self.message(repr(obj))

@contextmanager
def _disable_command_completion(self):
def _enable_multiline_completion(self):
completenames = self.completenames
try:
self.completenames = self.completedefault
self.completenames = self.complete_multiline_names
yield
finally:
self.completenames = completenames
Expand Down Expand Up @@ -753,7 +753,7 @@ def default(self, line):
buffer = line
if (code := codeop.compile_command(line + '\n', '<stdin>', 'single')) is None:
# Multi-line mode
with self._disable_command_completion():
with self._enable_multiline_completion():
buffer = line
continue_prompt = "... "
while (code := codeop.compile_command(buffer, '<stdin>', 'single')) is None:
Expand Down Expand Up @@ -996,6 +996,21 @@ def _complete_expression(self, text, line, begidx, endidx):
# Complete a simple name.
return [n for n in ns.keys() if n.startswith(text)]

def _complete_indentation(self, text, line, begidx, endidx):
try:
import readline
except ImportError:
return []
# Fill in spaces to form a 4-space indent
return [' ' * (4 - readline.get_begidx() % 4)]

def complete_multiline_names(self, text, line, begidx, endidx):
# If text is space-only, the user entered <tab> before any text.
# That normally means they want to indent the current line.
if not text.strip():
return self._complete_indentation(text, line, begidx, endidx)
return self.completedefault(text, line, begidx, endidx)

def completedefault(self, text, line, begidx, endidx):
if text.startswith("$"):
# Complete convenience variables
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4487,6 +4487,32 @@ def test_multiline_completion(self):

self.assertIn(b'42', output)

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

# \t should always complete a 4-space indent
# This piece of code will raise an IndentationError or a SyntaxError
# 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

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

output = run_pty(script, input)

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


def load_tests(loader, tests, pattern):
from test import test_pdb
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``<tab>`` at the beginning of the line in :mod:`pdb` multi-line input will fill in a 4-space indentation now, instead of inserting a ``\t`` character.
Copy link
Member

Choose a reason for hiding this comment

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

I'd add this to what's new in 3.14.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Loading