blob: 6edff9fe4b48c63dbf060ab2e4aeae82045c0c3e [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
2
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -04003from pybind11_tests import exceptions as m
Jason Rhinelanderd5981722017-07-28 21:38:23 -04004import pybind11_cross_module_tests as cm
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -04005
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02006
Dean Moldovan83e328f2017-06-09 00:44:49 +02007def test_std_exception(msg):
Dean Moldovan83e328f2017-06-09 00:44:49 +02008 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -04009 m.throw_std_exception()
Dean Moldovan83e328f2017-06-09 00:44:49 +020010 assert msg(excinfo.value) == "This exception was intentionally thrown."
11
12
Ivan Smirnov67b54892016-09-07 21:10:16 +010013def test_error_already_set(msg):
Ivan Smirnov67b54892016-09-07 21:10:16 +010014 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040015 m.throw_already_set(False)
Ivan Smirnov67b54892016-09-07 21:10:16 +010016 assert msg(excinfo.value) == "Unknown internal error occurred"
17
18 with pytest.raises(ValueError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040019 m.throw_already_set(True)
Ivan Smirnov67b54892016-09-07 21:10:16 +010020 assert msg(excinfo.value) == "foo"
21
22
Jason Rhinelanderd5981722017-07-28 21:38:23 -040023def test_cross_module_exceptions():
24 with pytest.raises(RuntimeError) as excinfo:
25 cm.raise_runtime_error()
26 assert str(excinfo.value) == "My runtime error"
27
28 with pytest.raises(ValueError) as excinfo:
29 cm.raise_value_error()
30 assert str(excinfo.value) == "My value error"
31
32 with pytest.raises(ValueError) as excinfo:
33 cm.throw_pybind_value_error()
34 assert str(excinfo.value) == "pybind11 value error"
35
36 with pytest.raises(TypeError) as excinfo:
37 cm.throw_pybind_type_error()
38 assert str(excinfo.value) == "pybind11 type error"
39
40 with pytest.raises(StopIteration) as excinfo:
41 cm.throw_stop_iteration()
42
43
Dean Moldovan135ba8d2016-09-10 11:58:02 +020044def test_python_call_in_catch():
Dean Moldovan135ba8d2016-09-10 11:58:02 +020045 d = {}
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040046 assert m.python_call_in_destructor(d) is True
Dean Moldovan135ba8d2016-09-10 11:58:02 +020047 assert d["good"] is True
48
49
Roman Miroshnychenko83a8a972017-04-02 23:38:50 +030050def test_exception_matches():
Yannick Jadoul97784da2019-05-12 23:35:49 +020051 assert m.exception_matches()
52 assert m.exception_matches_base()
53 assert m.modulenotfound_exception_matches_base()
Roman Miroshnychenko83a8a972017-04-02 23:38:50 +030054
55
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020056def test_custom(msg):
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040057 # Can we catch a MyException?
58 with pytest.raises(m.MyException) as excinfo:
59 m.throws1()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020060 assert msg(excinfo.value) == "this error should go to a custom type"
61
62 # Can we translate to standard Python exceptions?
63 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040064 m.throws2()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020065 assert msg(excinfo.value) == "this error should go to a standard Python exception"
66
67 # Can we handle unknown exceptions?
68 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040069 m.throws3()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020070 assert msg(excinfo.value) == "Caught an unknown exception!"
71
72 # Can we delegate to another handler by rethrowing?
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040073 with pytest.raises(m.MyException) as excinfo:
74 m.throws4()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020075 assert msg(excinfo.value) == "this error is rethrown"
76
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040077 # Can we fall-through to the default handler?
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020078 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040079 m.throws_logic_error()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020080 assert msg(excinfo.value) == "this error should fall through to the standard handler"
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040081
82 # Can we handle a helper-declared exception?
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040083 with pytest.raises(m.MyException5) as excinfo:
84 m.throws5()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040085 assert msg(excinfo.value) == "this is a helper-defined translated exception"
86
87 # Exception subclassing:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040088 with pytest.raises(m.MyException5) as excinfo:
89 m.throws5_1()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040090 assert msg(excinfo.value) == "MyException5 subclass"
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040091 assert isinstance(excinfo.value, m.MyException5_1)
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040092
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040093 with pytest.raises(m.MyException5_1) as excinfo:
94 m.throws5_1()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040095 assert msg(excinfo.value) == "MyException5 subclass"
96
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040097 with pytest.raises(m.MyException5) as excinfo:
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040098 try:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040099 m.throws5()
100 except m.MyException5_1:
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400101 raise RuntimeError("Exception error: caught child from parent")
102 assert msg(excinfo.value) == "this is a helper-defined translated exception"
Jason Rhinelander1682b672017-07-20 23:14:33 -0400103
104
105def test_nested_throws(capture):
106 """Tests nested (e.g. C++ -> Python -> C++) exception handling"""
107
108 def throw_myex():
109 raise m.MyException("nested error")
110
111 def throw_myex5():
112 raise m.MyException5("nested error 5")
113
114 # In the comments below, the exception is caught in the first step, thrown in the last step
115
116 # C++ -> Python
117 with capture:
118 m.try_catch(m.MyException5, throw_myex5)
119 assert str(capture).startswith("MyException5: nested error 5")
120
121 # Python -> C++ -> Python
122 with pytest.raises(m.MyException) as excinfo:
123 m.try_catch(m.MyException5, throw_myex)
124 assert str(excinfo.value) == "nested error"
125
126 def pycatch(exctype, f, *args):
127 try:
128 f(*args)
129 except m.MyException as e:
130 print(e)
131
132 # C++ -> Python -> C++ -> Python
133 with capture:
134 m.try_catch(
135 m.MyException5, pycatch, m.MyException, m.try_catch, m.MyException, throw_myex5)
136 assert str(capture).startswith("MyException5: nested error 5")
137
138 # C++ -> Python -> C++
139 with capture:
140 m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4)
141 assert capture == "this error is rethrown"
142
143 # Python -> C++ -> Python -> C++
144 with pytest.raises(m.MyException5) as excinfo:
145 m.try_catch(m.MyException, pycatch, m.MyException, m.throws5)
146 assert str(excinfo.value) == "this is a helper-defined translated exception"