Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e93643

Browse files
blueyedmiss-islington
authored andcommitted
bpo-35931: Gracefully handle any exception in pdb debug command (GH-12103)
This is relevant for `debug doesnotexist()`, which would crash with a NameError otherwise.
1 parent 1ceb3a3 commit 3e93643

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

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
@@ -1486,12 +1486,26 @@ def test_relative_imports_on_plain_module(self):
14861486
stdout, _ = self._run_pdb(['-m', self.module_name + '.runme'], commands)
14871487
self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout)
14881488

1489-
def test_syntaxerror_in_command(self):
1490-
commands = "print(\ndebug print("
1491-
stdout, _ = self.run_pdb_script("", commands)
1489+
def test_errors_in_command(self):
1490+
commands = "\n".join([
1491+
'print(',
1492+
'debug print(',
1493+
'debug doesnotexist',
1494+
'c',
1495+
])
1496+
stdout, _ = self.run_pdb_script('', commands + '\n')
1497+
14921498
self.assertEqual(stdout.splitlines()[1:], [
14931499
'(Pdb) *** SyntaxError: unexpected EOF while parsing',
1494-
'(Pdb) *** SyntaxError: unexpected EOF while parsing',
1500+
1501+
'(Pdb) ENTERING RECURSIVE DEBUGGER',
1502+
'*** SyntaxError: unexpected EOF while parsing',
1503+
'LEAVING RECURSIVE DEBUGGER',
1504+
1505+
'(Pdb) ENTERING RECURSIVE DEBUGGER',
1506+
'> <string>(1)<module>()',
1507+
"((Pdb)) *** NameError: name 'doesnotexist' is not defined",
1508+
'LEAVING RECURSIVE DEBUGGER',
14951509
'(Pdb) ',
14961510
])
14971511

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)