@@ -848,9 +848,7 @@ def test_post_mortem_chained():
848
848
... try:
849
849
... test_function_reraise()
850
850
... except Exception as e:
851
- ... # same as pdb.post_mortem(e), but with custom pdb instance.
852
- ... instance.reset()
853
- ... instance.interaction(None, e)
851
+ ... pdb._post_mortem(e, instance)
854
852
855
853
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
856
854
... 'exceptions',
@@ -907,24 +905,30 @@ def test_post_mortem_chained():
907
905
def test_post_mortem_cause_no_context ():
908
906
"""Test post mortem traceback debugging of chained exception
909
907
908
+ >>> def make_exc_with_stack(type_, *content, from_=None):
909
+ ... try:
910
+ ... raise type_(*content) from from_
911
+ ... except Exception as out:
912
+ ... return out
913
+ ...
914
+
910
915
>>> def main():
911
916
... try:
912
917
... raise ValueError('Context Not Shown')
913
918
... except Exception as e1:
914
- ... raise ValueError("With Cause") from TypeError( 'The Cause')
919
+ ... raise ValueError("With Cause") from make_exc_with_stack(TypeError, 'The Cause')
915
920
916
921
>>> def test_function():
917
922
... import pdb;
918
923
... instance = pdb.Pdb(nosigint=True, readrc=False)
919
924
... try:
920
925
... main()
921
926
... except Exception as e:
922
- ... # same as pdb.post_mortem(e), but with custom pdb instance.
923
- ... instance.reset()
924
- ... instance.interaction(None, e)
927
+ ... pdb._post_mortem(e, instance)
925
928
926
929
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
927
930
... 'exceptions',
931
+ ... 'exceptions 0',
928
932
... 'exceptions 1',
929
933
... 'up',
930
934
... 'down',
@@ -934,20 +938,23 @@ def test_post_mortem_cause_no_context():
934
938
... test_function()
935
939
... except ValueError:
936
940
... 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')
941
+ > <doctest test.test_pdb.test_post_mortem_cause_no_context[1 ]>(5)main()
942
+ -> raise ValueError("With Cause") from make_exc_with_stack(TypeError, 'The Cause')
939
943
(Pdb) exceptions
940
- 0 TypeError('The Cause')
941
- > 1 ValueError('With Cause')
944
+ 0 TypeError('The Cause')
945
+ > 1 ValueError('With Cause')
946
+ (Pdb) exceptions 0
947
+ > <doctest test.test_pdb.test_post_mortem_cause_no_context[0]>(3)make_exc_with_stack()
948
+ -> raise type_(*content) from from_
942
949
(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')
950
+ > <doctest test.test_pdb.test_post_mortem_cause_no_context[1 ]>(5)main()
951
+ -> raise ValueError("With Cause") from make_exc_with_stack(TypeError, 'The Cause')
945
952
(Pdb) up
946
- > <doctest test.test_pdb.test_post_mortem_cause_no_context[1 ]>(5)test_function()
953
+ > <doctest test.test_pdb.test_post_mortem_cause_no_context[2 ]>(5)test_function()
947
954
-> main()
948
955
(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')
956
+ > <doctest test.test_pdb.test_post_mortem_cause_no_context[1 ]>(5)main()
957
+ -> raise ValueError("With Cause") from make_exc_with_stack(TypeError, 'The Cause')
951
958
(Pdb) exit"""
952
959
953
960
@@ -971,9 +978,7 @@ def test_post_mortem_context_of_the_cause():
971
978
... try:
972
979
... main()
973
980
... except Exception as e:
974
- ... # same as pdb.post_mortem(e), but with custom pdb instance.
975
- ... instance.reset()
976
- ... instance.interaction(None, e)
981
+ ... pdb._post_mortem(e, instance)
977
982
978
983
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
979
984
... 'exceptions',
@@ -1046,9 +1051,7 @@ def test_post_mortem_from_none():
1046
1051
... try:
1047
1052
... main()
1048
1053
... except Exception as e:
1049
- ... # same as pdb.post_mortem(e), but with custom pdb instance.
1050
- ... instance.reset()
1051
- ... instance.interaction(None, e)
1054
+ ... pdb._post_mortem(e, instance)
1052
1055
1053
1056
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
1054
1057
... 'exceptions',
@@ -1066,6 +1069,64 @@ def test_post_mortem_from_none():
1066
1069
"""
1067
1070
1068
1071
1072
+ def test_post_mortem_from_no_stack ():
1073
+ """Test post mortem traceback debugging of chained exception
1074
+
1075
+ especially when one exception has no stack.
1076
+
1077
+ >>> def main():
1078
+ ... raise Exception() from Exception()
1079
+
1080
+
1081
+ >>> def test_function():
1082
+ ... import pdb;
1083
+ ... instance = pdb.Pdb(nosigint=True, readrc=False)
1084
+ ... try:
1085
+ ... main()
1086
+ ... except Exception as e:
1087
+ ... pdb._post_mortem(e, instance)
1088
+
1089
+ >>> with PdbTestInput( # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
1090
+ ... ["exceptions",
1091
+ ... "exceptions 0",
1092
+ ... "exit"],
1093
+ ... ):
1094
+ ... try:
1095
+ ... test_function()
1096
+ ... except ValueError:
1097
+ ... print('Correctly reraised.')
1098
+ > <doctest test.test_pdb.test_post_mortem_from_no_stack[0]>(2)main()
1099
+ -> raise Exception() from Exception()
1100
+ (Pdb) exceptions
1101
+ - Exception()
1102
+ > 1 Exception()
1103
+ (Pdb) exceptions 0
1104
+ *** This exception does not have a traceback, cannot jump to it
1105
+ (Pdb) exit
1106
+ """
1107
+
1108
+
1109
+ def test_post_mortem_single_no_stack ():
1110
+ """Test post mortem called when origin exception has no stack
1111
+
1112
+
1113
+ >>> def test_function():
1114
+ ... import pdb;
1115
+ ... instance = pdb.Pdb(nosigint=True, readrc=False)
1116
+ ... import sys
1117
+ ... sys.last_exc = Exception()
1118
+ ... pdb._post_mortem(sys.last_exc, instance)
1119
+
1120
+ >>> with PdbTestInput( # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
1121
+ ... []
1122
+ ... ):
1123
+ ... try:
1124
+ ... test_function()
1125
+ ... except ValueError as e:
1126
+ ... print(e)
1127
+ A valid traceback must be passed if no exception is being handled
1128
+ """
1129
+
1069
1130
def test_post_mortem_complex ():
1070
1131
"""Test post mortem traceback debugging of chained exception
1071
1132
@@ -1130,9 +1191,7 @@ def test_post_mortem_complex():
1130
1191
... try:
1131
1192
... main()
1132
1193
... except Exception as e:
1133
- ... # same as pdb.post_mortem(e), but with custom pdb instance.
1134
- ... instance.reset()
1135
- ... instance.interaction(None, e)
1194
+ ... pdb._post_mortem(e, instance)
1136
1195
1137
1196
>>> with PdbTestInput( # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
1138
1197
... ["exceptions",
0 commit comments