Skip to content

Commit 9f8282d

Browse files
gh-106670: Set convenience variable for post mortem debugging (#110493)
1 parent 0df772f commit 9f8282d

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Lib/pdb.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ def setup(self, f, tb):
304304
# cache it here to ensure that modifications are not overwritten.
305305
self.curframe_locals = self.curframe.f_locals
306306
self.set_convenience_variable(self.curframe, '_frame', self.curframe)
307+
308+
if self._chained_exceptions:
309+
self.set_convenience_variable(
310+
self.curframe,
311+
'_exception',
312+
self._chained_exceptions[self._chained_exception_index],
313+
)
314+
307315
return self.execRcLines()
308316

309317
# Can be executed earlier than 'setup' if desired

Lib/test/test_pdb.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,9 +859,11 @@ def test_post_mortem_chained():
859859
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
860860
... 'exceptions',
861861
... 'exceptions 0',
862+
... '$_exception',
862863
... 'up',
863864
... 'down',
864865
... 'exceptions 1',
866+
... '$_exception',
865867
... 'up',
866868
... 'down',
867869
... 'exceptions -1',
@@ -882,6 +884,8 @@ def test_post_mortem_chained():
882884
(Pdb) exceptions 0
883885
> <doctest test.test_pdb.test_post_mortem_chained[0]>(3)test_function_2()
884886
-> 1/0
887+
(Pdb) $_exception
888+
ZeroDivisionError('division by zero')
885889
(Pdb) up
886890
> <doctest test.test_pdb.test_post_mortem_chained[1]>(3)test_function_reraise()
887891
-> test_function_2()
@@ -891,6 +895,8 @@ def test_post_mortem_chained():
891895
(Pdb) exceptions 1
892896
> <doctest test.test_pdb.test_post_mortem_chained[1]>(5)test_function_reraise()
893897
-> raise ZeroDivisionError('reraised') from e
898+
(Pdb) $_exception
899+
ZeroDivisionError('reraised')
894900
(Pdb) up
895901
> <doctest test.test_pdb.test_post_mortem_chained[2]>(5)test_function()
896902
-> test_function_reraise()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
In :mod:`pdb`, set convenience variable ``$_exception`` for post mortem debugging.

0 commit comments

Comments
 (0)