Skip to content

Commit 1c4580d

Browse files
[3.7] bpo-35931: Gracefully handle any exception in pdb debug command (GH-12103) (GH-12285)
This is relevant for `debug doesnotexist()`, which would crash with a NameError otherwise. (cherry picked from commit 3e93643) Co-authored-by: Daniel Hahler <github@thequod.de> https://bugs.python.org/issue35931
1 parent 2c177ec commit 1c4580d

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

Lib/pdb.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,16 +1093,14 @@ def do_debug(self, arg):
10931093
sys.settrace(None)
10941094
globals = self.curframe.f_globals
10951095
locals = self.curframe_locals
1096-
try:
1097-
code = compile(arg, "<string>", "exec")
1098-
except SyntaxError:
1099-
exc_info = sys.exc_info()[:2]
1100-
self.error(traceback.format_exception_only(*exc_info)[-1].strip())
1101-
return
11021096
p = Pdb(self.completekey, self.stdin, self.stdout)
11031097
p.prompt = "(%s) " % self.prompt.strip()
11041098
self.message("ENTERING RECURSIVE DEBUGGER")
1105-
sys.call_tracing(p.run, (code, globals, locals))
1099+
try:
1100+
sys.call_tracing(p.run, (arg, globals, locals))
1101+
except Exception:
1102+
exc_info = sys.exc_info()[:2]
1103+
self.error(traceback.format_exception_only(*exc_info)[-1].strip())
11061104
self.message("LEAVING RECURSIVE DEBUGGER")
11071105
sys.settrace(self.trace_dispatch)
11081106
self.lastcmd = p.lastcmd

Lib/test/test_pdb.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,12 +1482,26 @@ def test_relative_imports_on_plain_module(self):
14821482
stdout, _ = self._run_pdb(['-m', self.module_name + '.runme'], commands)
14831483
self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout)
14841484

1485-
def test_syntaxerror_in_command(self):
1486-
commands = "print(\ndebug print("
1487-
stdout, _ = self.run_pdb_script("", commands)
1485+
def test_errors_in_command(self):
1486+
commands = "\n".join([
1487+
'print(',
1488+
'debug print(',
1489+
'debug doesnotexist',
1490+
'c',
1491+
])
1492+
stdout, _ = self.run_pdb_script('', commands + '\n')
1493+
14881494
self.assertEqual(stdout.splitlines()[1:], [
14891495
'(Pdb) *** SyntaxError: unexpected EOF while parsing',
1490-
'(Pdb) *** SyntaxError: unexpected EOF while parsing',
1496+
1497+
'(Pdb) ENTERING RECURSIVE DEBUGGER',
1498+
'*** SyntaxError: unexpected EOF while parsing',
1499+
'LEAVING RECURSIVE DEBUGGER',
1500+
1501+
'(Pdb) ENTERING RECURSIVE DEBUGGER',
1502+
'> <string>(1)<module>()',
1503+
"((Pdb)) *** NameError: name 'doesnotexist' is not defined",
1504+
'LEAVING RECURSIVE DEBUGGER',
14911505
'(Pdb) ',
14921506
])
14931507

Misc/NEWS.d/next/Library/2019-02-07-16-22-50.bpo-35931._63i7B.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The :mod:`pdb` ``debug`` command now gracefully handles all exceptions.

0 commit comments

Comments
 (0)