]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-103124: Multiline statement support for pdb (GH-103125)
authorTian Gao <gaogaotiantian@hotmail.com>
Thu, 15 Jun 2023 23:34:42 +0000 (16:34 -0700)
committerGitHub <noreply@github.com>
Thu, 15 Jun 2023 23:34:42 +0000 (23:34 +0000)
Lib/pdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2023-04-09-03-53-02.gh-issue-103124.JspiNN.rst [new file with mode: 0644]

index 1af511788749db46b1032deba40cc7db12f3c59f..3db3e6a5be1a7b5171e0e87f9da66bca261dd193 100755 (executable)
@@ -76,6 +76,7 @@ import bdb
 import dis
 import code
 import glob
+import codeop
 import pprint
 import signal
 import inspect
@@ -444,7 +445,30 @@ class Pdb(bdb.Bdb, cmd.Cmd):
         locals = self.curframe_locals
         globals = self.curframe.f_globals
         try:
-            code = compile(line + '\n', '<stdin>', 'single')
+            if (code := codeop.compile_command(line + '\n', '<stdin>', 'single')) is None:
+                # Multi-line mode
+                buffer = line
+                continue_prompt = "...   "
+                while (code := codeop.compile_command(buffer, '<stdin>', 'single')) is None:
+                    if self.use_rawinput:
+                        try:
+                            line = input(continue_prompt)
+                        except (EOFError, KeyboardInterrupt):
+                            self.lastcmd = ""
+                            print('\n')
+                            return
+                    else:
+                        self.stdout.write(continue_prompt)
+                        self.stdout.flush()
+                        line = self.stdin.readline()
+                        if not len(line):
+                            self.lastcmd = ""
+                            self.stdout.write('\n')
+                            self.stdout.flush()
+                            return
+                        else:
+                            line = line.rstrip('\r\n')
+                    buffer += '\n' + line
             save_stdout = sys.stdout
             save_stdin = sys.stdin
             save_displayhook = sys.displayhook
index 277400e152476feb062cd3fe8798b421584eca8b..a66953557e52dcbe4fc16c15f3ba6c5b20f57d8e 100644 (file)
@@ -389,7 +389,7 @@ def test_pdb_breakpoints_preserved_across_interactive_sessions():
     1   breakpoint   keep yes   at ...test_pdb.py:...
     2   breakpoint   keep yes   at ...test_pdb.py:...
     (Pdb) break pdb.find_function
-    Breakpoint 3 at ...pdb.py:97
+    Breakpoint 3 at ...pdb.py:...
     (Pdb) break
     Num Type         Disp Enb   Where
     1   breakpoint   keep yes   at ...test_pdb.py:...
@@ -1589,6 +1589,32 @@ def test_pdb_next_command_subiterator():
     (Pdb) continue
     """
 
+def test_pdb_multiline_statement():
+    """Test for multiline statement
+
+    >>> def test_function():
+    ...     import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
+    ...     pass
+
+    >>> with PdbTestInput([  # doctest: +NORMALIZE_WHITESPACE
+    ...     'def f(x):',
+    ...     '  return x * 2',
+    ...     '',
+    ...     'f(2)',
+    ...     'c'
+    ... ]):
+    ...     test_function()
+    > <doctest test.test_pdb.test_pdb_multiline_statement[0]>(3)test_function()
+    -> pass
+    (Pdb) def f(x):
+    ...     return x * 2
+    ...
+    (Pdb) f(2)
+    4
+    (Pdb) c
+    """
+
+
 def test_pdb_issue_20766():
     """Test for reference leaks when the SIGINT handler is set.
 
@@ -2362,7 +2388,7 @@ def bœr():
 
     def test_errors_in_command(self):
         commands = "\n".join([
-            'print(',
+            'print(]',
             'debug print(',
             'debug doesnotexist',
             'c',
@@ -2371,7 +2397,8 @@ def bœr():
 
         self.assertEqual(stdout.splitlines()[1:], [
             '-> pass',
-            '(Pdb) *** SyntaxError: \'(\' was never closed',
+            "(Pdb) *** SyntaxError: closing parenthesis ']' does not match opening "
+            "parenthesis '('",
 
             '(Pdb) ENTERING RECURSIVE DEBUGGER',
             '*** SyntaxError: \'(\' was never closed',
diff --git a/Misc/NEWS.d/next/Library/2023-04-09-03-53-02.gh-issue-103124.JspiNN.rst b/Misc/NEWS.d/next/Library/2023-04-09-03-53-02.gh-issue-103124.JspiNN.rst
new file mode 100644 (file)
index 0000000..022524b
--- /dev/null
@@ -0,0 +1 @@
+Added multiline statement support for :mod:`pdb`