@@ -377,8 +377,7 @@ def user_exception(self, frame, exc_info):
377
377
# stop when the debuggee is returning from such generators.
378
378
prefix = 'Internal ' if (not exc_traceback
379
379
and exc_type is StopIteration ) else ''
380
- self .message ('%s%s' % (prefix ,
381
- traceback .format_exception_only (exc_type , exc_value )[- 1 ].strip ()))
380
+ self .message ('%s%s' % (prefix , self ._format_exc (exc_value )))
382
381
self .interaction (frame , exc_traceback )
383
382
384
383
# General interaction function
@@ -399,7 +398,7 @@ def preloop(self):
399
398
displaying = self .displaying .get (self .curframe )
400
399
if displaying :
401
400
for expr , oldvalue in displaying .items ():
402
- newvalue , _ = self ._getval_except (expr )
401
+ newvalue = self ._getval_except (expr )
403
402
# check for identity first; this prevents custom __eq__ to
404
403
# be called at every loop, and also prevents instances whose
405
404
# fields are changed to be displayed
@@ -702,6 +701,9 @@ def do_break(self, arg, temporary = 0):
702
701
if comma > 0 :
703
702
# parse stuff after comma: "condition"
704
703
cond = arg [comma + 1 :].lstrip ()
704
+ if err := self ._compile_error_message (cond ):
705
+ self .error ('Invalid condition %s: %r' % (cond , err ))
706
+ return
705
707
arg = arg [:comma ].rstrip ()
706
708
# parse stuff before comma: [filename:]lineno | function
707
709
colon = arg .rfind (':' )
@@ -887,6 +889,9 @@ def do_condition(self, arg):
887
889
args = arg .split (' ' , 1 )
888
890
try :
889
891
cond = args [1 ]
892
+ if err := self ._compile_error_message (cond ):
893
+ self .error ('Invalid condition %s: %r' % (cond , err ))
894
+ return
890
895
except IndexError :
891
896
cond = None
892
897
try :
@@ -1246,16 +1251,15 @@ def _getval(self, arg):
1246
1251
def _getval_except (self , arg , frame = None ):
1247
1252
try :
1248
1253
if frame is None :
1249
- return eval (arg , self .curframe .f_globals , self .curframe_locals ), None
1254
+ return eval (arg , self .curframe .f_globals , self .curframe_locals )
1250
1255
else :
1251
- return eval (arg , frame .f_globals , frame .f_locals ), None
1256
+ return eval (arg , frame .f_globals , frame .f_locals )
1252
1257
except BaseException as exc :
1253
- err = traceback .format_exception_only (exc )[- 1 ].strip ()
1254
- return _rstr ('** raised %s **' % err ), exc
1258
+ return _rstr ('** raised %s **' % self ._format_exc (exc ))
1255
1259
1256
1260
def _error_exc (self ):
1257
- exc_info = sys .exc_info ()[: 2 ]
1258
- self .error (traceback . format_exception_only ( * exc_info )[ - 1 ]. strip ( ))
1261
+ exc = sys .exc_info ()[1 ]
1262
+ self .error (self . _format_exc ( exc ))
1259
1263
1260
1264
def _msg_val_func (self , arg , func ):
1261
1265
try :
@@ -1443,10 +1447,10 @@ def do_display(self, arg):
1443
1447
else :
1444
1448
self .message ('No expression is being displayed' )
1445
1449
else :
1446
- val , exc = self ._getval_except (arg )
1447
- if isinstance (exc , SyntaxError ):
1448
- self .message ('Unable to display %s: %r' % (arg , val ))
1450
+ if err := self ._compile_error_message (arg ):
1451
+ self .error ('Unable to display %s: %r' % (arg , err ))
1449
1452
else :
1453
+ val = self ._getval_except (arg )
1450
1454
self .displaying .setdefault (self .curframe , {})[arg ] = val
1451
1455
self .message ('display %s: %r' % (arg , val ))
1452
1456
@@ -1647,6 +1651,16 @@ def _run(self, target: Union[_ModuleTarget, _ScriptTarget]):
1647
1651
1648
1652
self .run (target .code )
1649
1653
1654
+ def _format_exc (self , exc : BaseException ):
1655
+ return traceback .format_exception_only (exc )[- 1 ].strip ()
1656
+
1657
+ def _compile_error_message (self , expr ):
1658
+ """Return the error message as string if compiling `expr` fails."""
1659
+ try :
1660
+ compile (expr , "<stdin>" , "eval" )
1661
+ except SyntaxError as exc :
1662
+ return _rstr (self ._format_exc (exc ))
1663
+ return ""
1650
1664
1651
1665
# Collect all command help into docstring, if not run with -OO
1652
1666
0 commit comments