Skip to content

Commit c6a4a37

Browse files
committed
do_debug: harden and fix for py38-dev (v3.8.0a2)
Based on python/cpython#12103.
1 parent 9a93c63 commit c6a4a37

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

pdb.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -653,24 +653,6 @@ def do_pp(self, arg):
653653
do_pp.__doc__ = pdb.Pdb.do_pp.__doc__
654654

655655
def do_debug(self, arg):
656-
# Compile the code already to not crash on SyntaxErrors.
657-
# Error handling is copied from pdb.Pdb.default.
658-
# This will be fixed in cpython 3.7.3 (bpo-35931).
659-
cmd = arg
660-
if isinstance(cmd, str):
661-
try:
662-
cmd = compile(cmd, "<string>", "exec")
663-
except SyntaxError:
664-
import traceback
665-
exc_info = sys.exc_info()[:2]
666-
msg = traceback.format_exception_only(*exc_info)[-1].strip()
667-
if hasattr(self, 'error'):
668-
self.error(msg)
669-
else:
670-
# For py27.
671-
print('***', msg, file=self.stdout)
672-
return
673-
674656
# this is a hack (as usual :-))
675657
#
676658
# inside the original do_debug, there is a call to the global "Pdb" to
@@ -696,7 +678,14 @@ def __init__(self, *args):
696678
'Pdb': PdbppWithConfig,
697679
}
698680
orig_do_debug = rebind_globals(do_debug_func, updateglobals)
699-
return orig_do_debug(self, cmd)
681+
682+
# Handle any exception, e.g. SyntaxErrors.
683+
# This is about to be improved in Python itself (3.8, 3.7.3?).
684+
try:
685+
return orig_do_debug(self, arg)
686+
except Exception:
687+
exc_info = sys.exc_info()[:2]
688+
self.error(traceback.format_exception_only(*exc_info)[-1].strip())
700689
do_debug.__doc__ = pdb.Pdb.do_debug.__doc__
701690

702691
def do_interact(self, arg):

testing/test_pdb.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,13 @@ def fn():
15441544

15451545

15461546
def test_syntaxerror_in_command():
1547+
# Python 3.8.0a2 handles the SyntaxError itself.
1548+
# Ref/followup: https://github.com/python/cpython/pull/12103
1549+
if sys.version_info == (3, 8, 0, "alpha", 2):
1550+
expected_debug_err = "\\*\\*\\* SyntaxError: .*"
1551+
else:
1552+
expected_debug_err = "ENTERING RECURSIVE DEBUGGER\n\\*\\*\\* SyntaxError: .*"
1553+
15471554
def f():
15481555
set_trace()
15491556

@@ -1555,9 +1562,9 @@ def f():
15551562
# print(
15561563
\\*\\*\\* SyntaxError: .*
15571564
# debug print(
1558-
\\*\\*\\* SyntaxError: .*
1565+
%s
15591566
# c
1560-
""")
1567+
""" % expected_debug_err)
15611568

15621569

15631570
def test_debug_with_overridden_continue():

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy