From 3b362b00718ef10f7b0d38b8753eb0a09f25a8d8 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 24 Mar 2023 18:57:00 -0700 Subject: [PATCH 1/7] Add syntax error check for display --- Lib/pdb.py | 22 ++++++++++++---------- Lib/test/test_pdb.py | 3 +++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 3543f53282db15..c4098eb29bf8c4 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -399,7 +399,7 @@ def preloop(self): 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 @@ def _getval(self, arg): 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 Exception as e: + err = traceback.format_exception_only(e)[-1].strip() + return _rstr('** raised %s **' % err), e def _error_exc(self): exc_info = sys.exc_info()[:2] @@ -1441,9 +1440,12 @@ def do_display(self, arg): for item in self.displaying.get(self.curframe, {}).items(): self.message('%s: %r' % item) 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(f'Unable to display "{arg}": SyntaxError') + 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 e96dc7fa1cf6e7..b954999575dfa0 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -586,6 +586,7 @@ def test_pdb_display_command(): ... a = 4 >>> with PdbTestInput([ # doctest: +ELLIPSIS + ... 'display +', ... 'display a', ... 'n', ... 'display', @@ -600,6 +601,8 @@ def test_pdb_display_command(): ... test_function() > (4)test_function() -> a = 1 + (Pdb) display + + Unable to display "+": SyntaxError (Pdb) display a display a: 0 (Pdb) n From 484b882ffc05a9828dc5ee20ce060c5a4e587443 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 24 Mar 2023 19:03:48 -0700 Subject: [PATCH 2/7] Check if any expression is being displayed --- Lib/pdb.py | 9 ++++++--- Lib/test/test_pdb.py | 3 +++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index c4098eb29bf8c4..e8b0699562af8c 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1436,9 +1436,12 @@ def do_display(self, arg): 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, exc = self._getval_except(arg) if isinstance(exc, SyntaxError): diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index b954999575dfa0..0480fb4bf919d9 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -587,6 +587,7 @@ def test_pdb_display_command(): >>> with PdbTestInput([ # doctest: +ELLIPSIS ... 'display +', + ... 'display', ... 'display a', ... 'n', ... 'display', @@ -603,6 +604,8 @@ def test_pdb_display_command(): -> a = 1 (Pdb) display + Unable to display "+": SyntaxError + (Pdb) display + No expression is being displayed (Pdb) display a display a: 0 (Pdb) n From 52a1cbfb6e1cbcc8cbb778332a412fe57eda7a23 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 25 Mar 2023 02:08:06 +0000 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-03-25-02-08-05.gh-issue-103023.Qfn7Hl.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-03-25-02-08-05.gh-issue-103023.Qfn7Hl.rst 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 00000000000000..c8dd2d2b112e8e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-03-25-02-08-05.gh-issue-103023.Qfn7Hl.rst @@ -0,0 +1 @@ +Added SyntaxError check on :mod:`pdb`'s display command From a843f9723b71ac54c0e0056364c16b84c71398db Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sat, 25 Mar 2023 21:44:23 -0700 Subject: [PATCH 4/7] Use BaseException to catch all exceptions --- Lib/pdb.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index e8b0699562af8c..5f5422b08ab1d8 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1249,9 +1249,9 @@ def _getval_except(self, arg, frame=None): return eval(arg, self.curframe.f_globals, self.curframe_locals), None else: return eval(arg, frame.f_globals, frame.f_locals), None - except Exception as e: - err = traceback.format_exception_only(e)[-1].strip() - return _rstr('** raised %s **' % err), e + 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] From b73669728691cc19f66b263b599cd094d43c6310 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 26 Mar 2023 10:25:04 -0700 Subject: [PATCH 5/7] Add the syntax error message to output --- Lib/pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 5f5422b08ab1d8..d402de1192f9e2 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1445,7 +1445,7 @@ def do_display(self, arg): else: val, exc = self._getval_except(arg) if isinstance(exc, SyntaxError): - self.message(f'Unable to display "{arg}": 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)) From 9a9762a717e4bc1ae0b412efa53f035d0f94db25 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sun, 26 Mar 2023 10:27:22 -0700 Subject: [PATCH 6/7] Update test --- Lib/test/test_pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 0480fb4bf919d9..ae9c5d73e2daa7 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -603,7 +603,7 @@ def test_pdb_display_command(): > (4)test_function() -> a = 1 (Pdb) display + - Unable to display "+": SyntaxError + Unable to display +: ** raised SyntaxError: invalid syntax ** (Pdb) display No expression is being displayed (Pdb) display a From c7810ffb1526c1f171dd3a65a75afa51f0c92a7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Mon, 27 Mar 2023 20:20:24 +0200 Subject: [PATCH 7/7] Make the news entry more descriptive --- .../Library/2023-03-25-02-08-05.gh-issue-103023.Qfn7Hl.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index c8dd2d2b112e8e..e7958f6f002055 100644 --- 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 @@ -1 +1,2 @@ -Added SyntaxError check on :mod:`pdb`'s display command +It's no longer possible to register expressions to display in +:class:`~pdb.Pdb` that raise :exc:`SyntaxError`. Patch by Tian Gao.