Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c45f12

Browse files
committed
Don't show context in from None
1 parent 5bb0140 commit 5c45f12

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

Lib/pdb.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,18 @@ def interaction(self, frame, tb_or_exc):
421421

422422
if isinstance(tb_or_exc, BaseException):
423423
tb_or_exc, exception = tb_or_exc.__traceback__, tb_or_exc
424-
self._chained_exceptions = [exception]
424+
self._chained_exceptions = []
425+
self._chained_exception_index = -1
425426
current = exception
426-
while current := (current.__cause__ or current.__context__):
427+
while current:
427428
self._chained_exception_index += 1
428429
self._chained_exceptions.insert(0, current)
430+
if current.__cause__:
431+
current = current.__cause__
432+
elif current.__context__ and not current.__suppress_context__:
433+
current = current.__context__
434+
else:
435+
current = None
429436
else:
430437
self._chained_exceptions = []
431438

Lib/test/test_pdb.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,45 @@ def test_post_mortem_context_of_the_cause():
10271027
"""
10281028

10291029

1030+
def test_post_mortem_from_none():
1031+
"""Test post mortem traceback debugging of chained exception
1032+
1033+
In particular that cause from None (which sets __supress_context__ to True)
1034+
does not show context.
1035+
1036+
1037+
>>> def main():
1038+
... try:
1039+
... raise TypeError('Context of the cause')
1040+
... except Exception as e1:
1041+
... raise ValueError("With Cause, and cause has context") from None
1042+
1043+
>>> def test_function():
1044+
... import pdb;
1045+
... instance = pdb.Pdb(nosigint=True, readrc=False)
1046+
... try:
1047+
... main()
1048+
... except Exception as e:
1049+
... # same as pdb.post_mortem(e), but with custom pdb instance.
1050+
... instance.reset()
1051+
... instance.interaction(None, e)
1052+
1053+
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
1054+
... 'exceptions',
1055+
... 'exit',
1056+
... ]):
1057+
... try:
1058+
... test_function()
1059+
... except ValueError:
1060+
... print('Correctly reraised.')
1061+
> <doctest test.test_pdb.test_post_mortem_from_none[0]>(5)main()
1062+
-> raise ValueError("With Cause, and cause has context") from None
1063+
(Pdb) exceptions
1064+
> 0 ValueError('With Cause, and cause has context')
1065+
(Pdb) exit
1066+
"""
1067+
1068+
10301069
def test_post_mortem():
10311070
"""Test post mortem traceback debugging.
10321071

0 commit comments

Comments
 (0)