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

Skip to content

Commit 5bb0140

Browse files
committed
Do not show context if cause
1 parent cc2dcab commit 5bb0140

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

Lib/pdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def interaction(self, frame, tb_or_exc):
423423
tb_or_exc, exception = tb_or_exc.__traceback__, tb_or_exc
424424
self._chained_exceptions = [exception]
425425
current = exception
426-
while current := current.__context__:
426+
while current := (current.__cause__ or current.__context__):
427427
self._chained_exception_index += 1
428428
self._chained_exceptions.insert(0, current)
429429
else:

Lib/test/test_pdb.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,129 @@ def test_post_mortem_chained():
904904
"""
905905

906906

907+
def test_post_mortem_cause_no_context():
908+
"""Test post mortem traceback debugging of chained exception
909+
910+
>>> def main():
911+
... try:
912+
... raise ValueError('Context Not Shown')
913+
... except Exception as e1:
914+
... raise ValueError("With Cause") from TypeError('The Cause')
915+
916+
>>> def test_function():
917+
... import pdb;
918+
... instance = pdb.Pdb(nosigint=True, readrc=False)
919+
... try:
920+
... main()
921+
... except Exception as e:
922+
... # same as pdb.post_mortem(e), but with custom pdb instance.
923+
... instance.reset()
924+
... instance.interaction(None, e)
925+
926+
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
927+
... 'exceptions',
928+
... 'exceptions 1',
929+
... 'up',
930+
... 'down',
931+
... 'exit',
932+
... ]):
933+
... try:
934+
... test_function()
935+
... except ValueError:
936+
... print('Ok.')
937+
> <doctest test.test_pdb.test_post_mortem_cause_no_context[0]>(5)main()
938+
-> raise ValueError("With Cause") from TypeError('The Cause')
939+
(Pdb) exceptions
940+
0 TypeError('The Cause')
941+
> 1 ValueError('With Cause')
942+
(Pdb) exceptions 1
943+
> <doctest test.test_pdb.test_post_mortem_cause_no_context[0]>(5)main()
944+
-> raise ValueError("With Cause") from TypeError('The Cause')
945+
(Pdb) up
946+
> <doctest test.test_pdb.test_post_mortem_cause_no_context[1]>(5)test_function()
947+
-> main()
948+
(Pdb) down
949+
> <doctest test.test_pdb.test_post_mortem_cause_no_context[0]>(5)main()
950+
-> raise ValueError("With Cause") from TypeError('The Cause')
951+
(Pdb) exit"""
952+
953+
954+
def test_post_mortem_context_of_the_cause():
955+
"""Test post mortem traceback debugging of chained exception
956+
957+
958+
>>> def main():
959+
... try:
960+
... raise TypeError('Context of the cause')
961+
... except Exception as e1:
962+
... try:
963+
... raise ValueError('Root Cause')
964+
... except Exception as e2:
965+
... ex = e2
966+
... raise ValueError("With Cause, and cause has context") from ex
967+
968+
>>> def test_function():
969+
... import pdb;
970+
... instance = pdb.Pdb(nosigint=True, readrc=False)
971+
... try:
972+
... main()
973+
... except Exception as e:
974+
... # same as pdb.post_mortem(e), but with custom pdb instance.
975+
... instance.reset()
976+
... instance.interaction(None, e)
977+
978+
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
979+
... 'exceptions',
980+
... 'exceptions 2',
981+
... 'up',
982+
... 'down',
983+
... 'exceptions 3',
984+
... 'up',
985+
... 'down',
986+
... 'exceptions 4',
987+
... 'up',
988+
... 'down',
989+
... 'exit',
990+
... ]):
991+
... try:
992+
... test_function()
993+
... except ValueError:
994+
... print('Correctly reraised.')
995+
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[0]>(9)main()
996+
-> raise ValueError("With Cause, and cause has context") from ex
997+
(Pdb) exceptions
998+
0 TypeError('Context of the cause')
999+
1 ValueError('Root Cause')
1000+
> 2 ValueError('With Cause, and cause has context')
1001+
(Pdb) exceptions 2
1002+
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[0]>(9)main()
1003+
-> raise ValueError("With Cause, and cause has context") from ex
1004+
(Pdb) up
1005+
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[1]>(5)test_function()
1006+
-> main()
1007+
(Pdb) down
1008+
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[0]>(9)main()
1009+
-> raise ValueError("With Cause, and cause has context") from ex
1010+
(Pdb) exceptions 3
1011+
*** No exception with that number
1012+
(Pdb) up
1013+
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[1]>(5)test_function()
1014+
-> main()
1015+
(Pdb) down
1016+
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[0]>(9)main()
1017+
-> raise ValueError("With Cause, and cause has context") from ex
1018+
(Pdb) exceptions 4
1019+
*** No exception with that number
1020+
(Pdb) up
1021+
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[1]>(5)test_function()
1022+
-> main()
1023+
(Pdb) down
1024+
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[0]>(9)main()
1025+
-> raise ValueError("With Cause, and cause has context") from ex
1026+
(Pdb) exit
1027+
"""
1028+
1029+
9071030
def test_post_mortem():
9081031
"""Test post mortem traceback debugging.
9091032

0 commit comments

Comments
 (0)