Skip to content

Commit 3606753

Browse files
gaogaotiantianambv
andauthored
gh-103023: Add SyntaxError check in pdb's display command (#103024)
Co-authored-by: Łukasz Langa <[email protected]>
1 parent 2cdc518 commit 3606753

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

Lib/pdb.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def preloop(self):
399399
displaying = self.displaying.get(self.curframe)
400400
if displaying:
401401
for expr, oldvalue in displaying.items():
402-
newvalue = self._getval_except(expr)
402+
newvalue, _ = self._getval_except(expr)
403403
# check for identity first; this prevents custom __eq__ to
404404
# be called at every loop, and also prevents instances whose
405405
# fields are changed to be displayed
@@ -1246,13 +1246,12 @@ def _getval(self, arg):
12461246
def _getval_except(self, arg, frame=None):
12471247
try:
12481248
if frame is None:
1249-
return eval(arg, self.curframe.f_globals, self.curframe_locals)
1249+
return eval(arg, self.curframe.f_globals, self.curframe_locals), None
12501250
else:
1251-
return eval(arg, frame.f_globals, frame.f_locals)
1252-
except:
1253-
exc_info = sys.exc_info()[:2]
1254-
err = traceback.format_exception_only(*exc_info)[-1].strip()
1255-
return _rstr('** raised %s **' % err)
1251+
return eval(arg, frame.f_globals, frame.f_locals), None
1252+
except BaseException as exc:
1253+
err = traceback.format_exception_only(exc)[-1].strip()
1254+
return _rstr('** raised %s **' % err), exc
12561255

12571256
def _error_exc(self):
12581257
exc_info = sys.exc_info()[:2]
@@ -1437,13 +1436,19 @@ def do_display(self, arg):
14371436
Without expression, list all display expressions for the current frame.
14381437
"""
14391438
if not arg:
1440-
self.message('Currently displaying:')
1441-
for item in self.displaying.get(self.curframe, {}).items():
1442-
self.message('%s: %r' % item)
1439+
if self.displaying:
1440+
self.message('Currently displaying:')
1441+
for item in self.displaying.get(self.curframe, {}).items():
1442+
self.message('%s: %r' % item)
1443+
else:
1444+
self.message('No expression is being displayed')
14431445
else:
1444-
val = self._getval_except(arg)
1445-
self.displaying.setdefault(self.curframe, {})[arg] = val
1446-
self.message('display %s: %r' % (arg, val))
1446+
val, exc = self._getval_except(arg)
1447+
if isinstance(exc, SyntaxError):
1448+
self.message('Unable to display %s: %r' % (arg, val))
1449+
else:
1450+
self.displaying.setdefault(self.curframe, {})[arg] = val
1451+
self.message('display %s: %r' % (arg, val))
14471452

14481453
complete_display = _complete_expression
14491454

Lib/test/test_pdb.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ def test_pdb_display_command():
586586
... a = 4
587587
588588
>>> with PdbTestInput([ # doctest: +ELLIPSIS
589+
... 'display +',
590+
... 'display',
589591
... 'display a',
590592
... 'n',
591593
... 'display',
@@ -600,6 +602,10 @@ def test_pdb_display_command():
600602
... test_function()
601603
> <doctest test.test_pdb.test_pdb_display_command[0]>(4)test_function()
602604
-> a = 1
605+
(Pdb) display +
606+
Unable to display +: ** raised SyntaxError: invalid syntax **
607+
(Pdb) display
608+
No expression is being displayed
603609
(Pdb) display a
604610
display a: 0
605611
(Pdb) n
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
It's no longer possible to register expressions to display in
2+
:class:`~pdb.Pdb` that raise :exc:`SyntaxError`. Patch by Tian Gao.

0 commit comments

Comments
 (0)