From 36067532467ec58c87ac9c140e555e1866431981 Mon Sep 17 00:00:00 2001 From: gaogaotiantian Date: Mon, 27 Mar 2023 13:37:22 -0700 Subject: gh-103023: Add SyntaxError check in pdb's `display` command (#103024) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ɓukasz Langa --- Lib/pdb.py | 31 +++++++++++++--------- Lib/test/test_pdb.py | 6 +++++ .../2023-03-25-02-08-05.gh-issue-103023.Qfn7Hl.rst | 2 ++ 3 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-03-25-02-08-05.gh-issue-103023.Qfn7Hl.rst diff --git a/Lib/pdb.py b/Lib/pdb.py index 3543f53..d402de1 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -399,7 +399,7 @@ class Pdb(bdb.Bdb, cmd.Cmd): displaying = self.displaying.get(self.curframe) if displaying: for expr, oldvalue in displaying.items(): - newvalue = self._getval_except(expr) + newvalue, _ = self._getval_except(expr) # check for identity first; this prevents custom __eq__ to # be called at every loop, and also prevents instances whose # fields are changed to be displayed @@ -1246,13 +1246,12 @@ class Pdb(bdb.Bdb, cmd.Cmd): def _getval_except(self, arg, frame=None): try: if frame is None: - return eval(arg, self.curframe.f_globals, self.curframe_locals) + return eval(arg, self.curframe.f_globals, self.curframe_locals), None else: - return eval(arg, frame.f_globals, frame.f_locals) - except: - exc_info = sys.exc_info()[:2] - err = traceback.format_exception_only(*exc_info)[-1].strip() - return _rstr('** raised %s **' % err) + return eval(arg, frame.f_globals, frame.f_locals), None + except BaseException as exc: + err = traceback.format_exception_only(exc)[-1].strip() + return _rstr('** raised %s **' % err), exc def _error_exc(self): exc_info = sys.exc_info()[:2] @@ -1437,13 +1436,19 @@ class Pdb(bdb.Bdb, cmd.Cmd): Without expression, list all display expressions for the current frame. """ if not arg: - self.message('Currently displaying:') - for item in self.displaying.get(self.curframe, {}).items(): - self.message('%s: %r' % item) + if self.displaying: + self.message('Currently displaying:') + for item in self.displaying.get(self.curframe, {}).items(): + self.message('%s: %r' % item) + else: + self.message('No expression is being displayed') else: - val = self._getval_except(arg) - self.displaying.setdefault(self.curframe, {})[arg] = val - self.message('display %s: %r' % (arg, val)) + val, exc = self._getval_except(arg) + if isinstance(exc, SyntaxError): + self.message('Unable to display %s: %r' % (arg, val)) + else: + self.displaying.setdefault(self.curframe, {})[arg] = val + self.message('display %s: %r' % (arg, val)) complete_display = _complete_expression diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index e96dc7f..ae9c5d7 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -586,6 +586,8 @@ def test_pdb_display_command(): ... a = 4 >>> with PdbTestInput([ # doctest: +ELLIPSIS + ... 'display +', + ... 'display', ... 'display a', ... 'n', ... 'display', @@ -600,6 +602,10 @@ def test_pdb_display_command(): ... test_function() > (4)test_function() -> a = 1 + (Pdb) display + + Unable to display +: ** raised SyntaxError: invalid syntax ** + (Pdb) display + No expression is being displayed (Pdb) display a display a: 0 (Pdb) n diff --git a/Misc/NEWS.d/next/Library/2023-03-25-02-08-05.gh-issue-103023.Qfn7Hl.rst b/Misc/NEWS.d/next/Library/2023-03-25-02-08-05.gh-issue-103023.Qfn7Hl.rst new file mode 100644 index 0000000..e7958f6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-03-25-02-08-05.gh-issue-103023.Qfn7Hl.rst @@ -0,0 +1,2 @@ +It's no longer possible to register expressions to display in +:class:`~pdb.Pdb` that raise :exc:`SyntaxError`. Patch by Tian Gao. -- cgit v0.12