@@ -907,11 +907,18 @@ def test_post_mortem_chained():
907
907
def test_post_mortem_cause_no_context ():
908
908
"""Test post mortem traceback debugging of chained exception
909
909
910
+ >>> def make_ex_with_stack(type_, *content, from_=None):
911
+ ... try:
912
+ ... raise type_(*content) from from_
913
+ ... except Exception as out:
914
+ ... return out
915
+ ...
916
+
910
917
>>> def main():
911
918
... try:
912
919
... raise ValueError('Context Not Shown')
913
920
... except Exception as e1:
914
- ... raise ValueError("With Cause") from TypeError( 'The Cause')
921
+ ... raise ValueError("With Cause") from make_ex_with_stack(TypeError, 'The Cause')
915
922
916
923
>>> def test_function():
917
924
... import pdb;
@@ -925,6 +932,7 @@ def test_post_mortem_cause_no_context():
925
932
926
933
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
927
934
... 'exceptions',
935
+ ... 'exceptions 0',
928
936
... 'exceptions 1',
929
937
... 'up',
930
938
... 'down',
@@ -934,20 +942,23 @@ def test_post_mortem_cause_no_context():
934
942
... test_function()
935
943
... except ValueError:
936
944
... 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')
945
+ > <doctest test.test_pdb.test_post_mortem_cause_no_context[1 ]>(5)main()
946
+ -> raise ValueError("With Cause") from make_ex_with_stack(TypeError, 'The Cause')
939
947
(Pdb) exceptions
940
- 0 TypeError('The Cause')
941
- > 1 ValueError('With Cause')
948
+ 0 TypeError('The Cause')
949
+ > 1 ValueError('With Cause')
950
+ (Pdb) exceptions 0
951
+ > <doctest test.test_pdb.test_post_mortem_cause_no_context[0]>(3)make_ex_with_stack()
952
+ -> raise type_(*content) from from_
942
953
(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')
954
+ > <doctest test.test_pdb.test_post_mortem_cause_no_context[1 ]>(5)main()
955
+ -> raise ValueError("With Cause") from make_ex_with_stack(TypeError, 'The Cause')
945
956
(Pdb) up
946
- > <doctest test.test_pdb.test_post_mortem_cause_no_context[1 ]>(5)test_function()
957
+ > <doctest test.test_pdb.test_post_mortem_cause_no_context[2 ]>(5)test_function()
947
958
-> main()
948
959
(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')
960
+ > <doctest test.test_pdb.test_post_mortem_cause_no_context[1 ]>(5)main()
961
+ -> raise ValueError("With Cause") from make_ex_with_stack(TypeError, 'The Cause')
951
962
(Pdb) exit"""
952
963
953
964
@@ -1066,6 +1077,65 @@ def test_post_mortem_from_none():
1066
1077
"""
1067
1078
1068
1079
1080
+ def test_post_mortem_from_no_stack ():
1081
+ """Test post mortem traceback debugging of chained exception
1082
+
1083
+ especially when one exception has not stack.
1084
+
1085
+ >>> def main():
1086
+ ... raise Exception() from Exception()
1087
+
1088
+
1089
+ >>> def test_function():
1090
+ ... import pdb;
1091
+ ... instance = pdb.Pdb(nosigint=True, readrc=False)
1092
+ ... try:
1093
+ ... main()
1094
+ ... except Exception as e:
1095
+ ... # same as pdb.post_mortem(e), but with custom pdb instance.
1096
+ ... instance.reset()
1097
+ ... instance.interaction(None, e)
1098
+
1099
+ >>> with PdbTestInput( # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
1100
+ ... ["exceptions",
1101
+ ... "exit"],
1102
+ ... ):
1103
+ ... try:
1104
+ ... test_function()
1105
+ ... except ValueError:
1106
+ ... print('Correctly reraised.')
1107
+ > <doctest test.test_pdb.test_post_mortem_from_no_stack[0]>(2)main()
1108
+ -> raise Exception() from Exception()
1109
+ (Pdb) exceptions
1110
+ > 0 Exception()
1111
+ (Pdb) exit
1112
+ """
1113
+
1114
+
1115
+ def test_post_mortem_single_no_stack ():
1116
+ """Test post mortem called when origin exception has not stack
1117
+
1118
+
1119
+ >>> def test_function():
1120
+ ... import pdb;
1121
+ ... instance = pdb.Pdb(nosigint=True, readrc=False)
1122
+ ... import sys
1123
+ ... sys.last_exc = Exception()
1124
+ ... # same as pdb.post_mortem(e), but with custom pdb instance.
1125
+ ... instance.reset()
1126
+ ... instance.interaction(None, sys.last_exc)
1127
+
1128
+ >>> with PdbTestInput( # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
1129
+ ... []
1130
+ ... ):
1131
+ ... try:
1132
+ ... test_function()
1133
+ ... except ValueError as e:
1134
+ ... print(e)
1135
+ A valid traceback must be passed if no exception is being handled
1136
+ """
1137
+
1138
+
1069
1139
def test_post_mortem_complex ():
1070
1140
"""Test post mortem traceback debugging of chained exception
1071
1141
0 commit comments