@@ -827,7 +827,7 @@ def test_convenience_variables():
827
827
"""
828
828
829
829
830
- def test_post_mortem_chained ():
830
+ def test_post_mortem_simple_chained ():
831
831
"""Test post mortem traceback debugging of chained exception
832
832
833
833
>>> def test_function_2():
@@ -870,40 +870,131 @@ def test_post_mortem_chained():
870
870
... except ZeroDivisionError:
871
871
... print('Correctly reraised.')
872
872
Exception!
873
- > <doctest test.test_pdb.test_post_mortem_chained [1]>(5)test_function_reraise()
873
+ > <doctest test.test_pdb.test_post_mortem_simple_chained [1]>(5)test_function_reraise()
874
874
-> raise ZeroDivisionError('reraised') from e
875
875
(Pdb) exceptions
876
876
0 ZeroDivisionError('division by zero')
877
877
> 1 ZeroDivisionError('reraised')
878
878
(Pdb) exceptions 0
879
- > <doctest test.test_pdb.test_post_mortem_chained [0]>(3)test_function_2()
879
+ > <doctest test.test_pdb.test_post_mortem_simple_chained [0]>(3)test_function_2()
880
880
-> 1/0
881
881
(Pdb) up
882
- > <doctest test.test_pdb.test_post_mortem_chained [1]>(3)test_function_reraise()
882
+ > <doctest test.test_pdb.test_post_mortem_simple_chained [1]>(3)test_function_reraise()
883
883
-> test_function_2()
884
884
(Pdb) down
885
- > <doctest test.test_pdb.test_post_mortem_chained [0]>(3)test_function_2()
885
+ > <doctest test.test_pdb.test_post_mortem_simple_chained [0]>(3)test_function_2()
886
886
-> 1/0
887
887
(Pdb) exceptions 1
888
- > <doctest test.test_pdb.test_post_mortem_chained [1]>(5)test_function_reraise()
888
+ > <doctest test.test_pdb.test_post_mortem_simple_chained [1]>(5)test_function_reraise()
889
889
-> raise ZeroDivisionError('reraised') from e
890
890
(Pdb) up
891
- > <doctest test.test_pdb.test_post_mortem_chained [2]>(5)test_function()
891
+ > <doctest test.test_pdb.test_post_mortem_simple_chained [2]>(5)test_function()
892
892
-> test_function_reraise()
893
893
(Pdb) down
894
- > <doctest test.test_pdb.test_post_mortem_chained [1]>(5)test_function_reraise()
894
+ > <doctest test.test_pdb.test_post_mortem_simple_chained [1]>(5)test_function_reraise()
895
895
-> raise ZeroDivisionError('reraised') from e
896
896
(Pdb) exceptions -1
897
897
*** No exception with that number
898
898
(Pdb) exceptions 3
899
899
*** No exception with that number
900
900
(Pdb) up
901
- > <doctest test.test_pdb.test_post_mortem_chained [2]>(5)test_function()
901
+ > <doctest test.test_pdb.test_post_mortem_simple_chained [2]>(5)test_function()
902
902
-> test_function_reraise()
903
903
(Pdb) exit
904
904
"""
905
905
906
906
907
+ def test_post_mortem_context_and_cause ():
908
+ """Test post mortem traceback debugging of chained exception
909
+
910
+ >>> def cause():
911
+ ... try:
912
+ ... raise ValueError("Cause Leaf")
913
+ ... except Exception as e:
914
+ ... raise ValueError("With Cause") from e
915
+
916
+ >>> def context():
917
+ ... try:
918
+ ... raise ValueError("Context Leaf")
919
+ ... except Exception as e:
920
+ ... raise ValueError("With Context") from e
921
+
922
+ >>> def main():
923
+ ... try:
924
+ ... context()
925
+ ... except Exception as e1:
926
+ ... try:
927
+ ... cause()
928
+ ... except Exception as e2:
929
+ ... ex = e2
930
+ ... raise ValueError("With Context and With Cause") from ex
931
+
932
+ >>> def test_function():
933
+ ... import pdb;
934
+ ... instance = pdb.Pdb(nosigint=True, readrc=False)
935
+ ... try:
936
+ ... main()
937
+ ... except Exception as e:
938
+ ... # same as pdb.post_mortem(e), but with custom pdb instance.
939
+ ... instance.reset()
940
+ ... instance.interaction(None, e)
941
+
942
+ >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
943
+ ... 'exceptions',
944
+ ... 'exceptions 2',
945
+ ... 'up',
946
+ ... 'down',
947
+ ... 'exceptions 3',
948
+ ... 'up',
949
+ ... 'down',
950
+ ... 'exceptions 4',
951
+ ... 'up',
952
+ ... 'down',
953
+ ... 'exit',
954
+ ... ]):
955
+ ... try:
956
+ ... test_function()
957
+ ... except ValueError:
958
+ ... print('Correctly reraised.')
959
+ > <doctest test.test_pdb.test_post_mortem_context_and_cause[2]>(9)main()
960
+ -> raise ValueError("With Context and With Cause") from ex
961
+ (Pdb) exceptions
962
+ 0 ValueError('Cause Leaf')
963
+ 1 ValueError('Context Leaf')
964
+ 2 ValueError('With Cause')
965
+ 3 ValueError('With Context')
966
+ > 4 ValueError('With Context and With Cause')
967
+ (Pdb) exceptions 2
968
+ > <doctest test.test_pdb.test_post_mortem_context_and_cause[0]>(5)cause()
969
+ -> raise ValueError("With Cause") from e
970
+ (Pdb) up
971
+ > <doctest test.test_pdb.test_post_mortem_context_and_cause[2]>(6)main()
972
+ -> cause()
973
+ (Pdb) down
974
+ > <doctest test.test_pdb.test_post_mortem_context_and_cause[0]>(5)cause()
975
+ -> raise ValueError("With Cause") from e
976
+ (Pdb) exceptions 3
977
+ > <doctest test.test_pdb.test_post_mortem_context_and_cause[1]>(5)context()
978
+ -> raise ValueError("With Context") from e
979
+ (Pdb) up
980
+ > <doctest test.test_pdb.test_post_mortem_context_and_cause[2]>(3)main()
981
+ -> context()
982
+ (Pdb) down
983
+ > <doctest test.test_pdb.test_post_mortem_context_and_cause[1]>(5)context()
984
+ -> raise ValueError("With Context") from e
985
+ (Pdb) exceptions 4
986
+ > <doctest test.test_pdb.test_post_mortem_context_and_cause[2]>(9)main()
987
+ -> raise ValueError("With Context and With Cause") from ex
988
+ (Pdb) up
989
+ > <doctest test.test_pdb.test_post_mortem_context_and_cause[3]>(5)test_function()
990
+ -> main()
991
+ (Pdb) down
992
+ > <doctest test.test_pdb.test_post_mortem_context_and_cause[2]>(9)main()
993
+ -> raise ValueError("With Context and With Cause") from ex
994
+ (Pdb) exit
995
+ """
996
+
997
+
907
998
def test_post_mortem ():
908
999
"""Test post mortem traceback debugging.
909
1000
0 commit comments