From 9f8282de6bdc3e1f976318821ff151ed45fedc56 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 9 Oct 2023 01:17:24 -0700 Subject: gh-106670: Set convenience variable for post mortem debugging (#110493) --- Lib/pdb.py | 8 ++++++++ Lib/test/test_pdb.py | 6 ++++++ .../next/Library/2023-10-07-00-18-40.gh-issue-106670.kCGyRc.rst | 1 + 3 files changed, 15 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-10-07-00-18-40.gh-issue-106670.kCGyRc.rst diff --git a/Lib/pdb.py b/Lib/pdb.py index fd62d24..2cbd6f6 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -304,6 +304,14 @@ class Pdb(bdb.Bdb, cmd.Cmd): # cache it here to ensure that modifications are not overwritten. self.curframe_locals = self.curframe.f_locals self.set_convenience_variable(self.curframe, '_frame', self.curframe) + + if self._chained_exceptions: + self.set_convenience_variable( + self.curframe, + '_exception', + self._chained_exceptions[self._chained_exception_index], + ) + return self.execRcLines() # Can be executed earlier than 'setup' if desired diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 8fed1d0..b1652e9 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -859,9 +859,11 @@ def test_post_mortem_chained(): >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE ... 'exceptions', ... 'exceptions 0', + ... '$_exception', ... 'up', ... 'down', ... 'exceptions 1', + ... '$_exception', ... 'up', ... 'down', ... 'exceptions -1', @@ -882,6 +884,8 @@ def test_post_mortem_chained(): (Pdb) exceptions 0 > (3)test_function_2() -> 1/0 + (Pdb) $_exception + ZeroDivisionError('division by zero') (Pdb) up > (3)test_function_reraise() -> test_function_2() @@ -891,6 +895,8 @@ def test_post_mortem_chained(): (Pdb) exceptions 1 > (5)test_function_reraise() -> raise ZeroDivisionError('reraised') from e + (Pdb) $_exception + ZeroDivisionError('reraised') (Pdb) up > (5)test_function() -> test_function_reraise() diff --git a/Misc/NEWS.d/next/Library/2023-10-07-00-18-40.gh-issue-106670.kCGyRc.rst b/Misc/NEWS.d/next/Library/2023-10-07-00-18-40.gh-issue-106670.kCGyRc.rst new file mode 100644 index 0000000..3a06616 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-10-07-00-18-40.gh-issue-106670.kCGyRc.rst @@ -0,0 +1 @@ +In :mod:`pdb`, set convenience variable ``$_exception`` for post mortem debugging. -- cgit v0.12