6 from pybind11_tests
import exceptions
as m
7 import pybind11_cross_module_tests
as cm
11 with pytest.raises(RuntimeError)
as excinfo:
12 m.throw_std_exception()
13 assert msg(excinfo.value) ==
"This exception was intentionally thrown."
17 with pytest.raises(RuntimeError)
as excinfo:
18 m.throw_already_set(
False)
19 assert msg(excinfo.value) ==
"Unknown internal error occurred"
21 with pytest.raises(ValueError)
as excinfo:
22 m.throw_already_set(
True)
23 assert msg(excinfo.value) ==
"foo"
27 with pytest.raises(RuntimeError)
as excinfo:
28 cm.raise_runtime_error()
29 assert str(excinfo.value) ==
"My runtime error"
31 with pytest.raises(ValueError)
as excinfo:
32 cm.raise_value_error()
33 assert str(excinfo.value) ==
"My value error"
35 with pytest.raises(ValueError)
as excinfo:
36 cm.throw_pybind_value_error()
37 assert str(excinfo.value) ==
"pybind11 value error"
39 with pytest.raises(TypeError)
as excinfo:
40 cm.throw_pybind_type_error()
41 assert str(excinfo.value) ==
"pybind11 type error"
43 with pytest.raises(StopIteration)
as excinfo:
44 cm.throw_stop_iteration()
49 assert m.python_call_in_destructor(d)
is True
50 assert d[
"good"]
is True
54 unraisable =
"PytestUnraisableExceptionWarning"
56 dec = pytest.mark.filterwarnings(
"ignore::pytest.{}".format(unraisable))
62 @ignore_pytest_unraisable_warning
67 if hasattr(sys,
"unraisablehook"):
70 default_hook = sys.__unraisablehook__
72 def hook(unraisable_hook_args):
73 exc_type, exc_value, exc_tb, err_msg, obj = unraisable_hook_args
74 if obj ==
"already_set demo":
76 default_hook(unraisable_hook_args)
80 monkeypatch.setattr(sys,
"unraisablehook", hook)
82 assert m.python_alreadyset_in_destructor(
"already_set demo")
is True
84 assert triggered[0]
is True
86 _, captured_stderr = capsys.readouterr()
88 assert "ignored" in captured_stderr
and "already_set demo" in captured_stderr
92 assert m.exception_matches()
93 assert m.exception_matches_base()
94 assert m.modulenotfound_exception_matches_base()
99 with pytest.raises(m.MyException)
as excinfo:
101 assert msg(excinfo.value) ==
"this error should go to a custom type"
104 with pytest.raises(RuntimeError)
as excinfo:
106 assert msg(excinfo.value) ==
"this error should go to a standard Python exception"
109 with pytest.raises(RuntimeError)
as excinfo:
111 assert msg(excinfo.value) ==
"Caught an unknown exception!"
114 with pytest.raises(m.MyException)
as excinfo:
116 assert msg(excinfo.value) ==
"this error is rethrown"
119 with pytest.raises(RuntimeError)
as excinfo:
120 m.throws_logic_error()
122 msg(excinfo.value) ==
"this error should fall through to the standard handler"
126 with pytest.raises(OverflowError)
as excinfo:
127 m.throws_overflow_error()
130 with pytest.raises(m.MyException5)
as excinfo:
132 assert msg(excinfo.value) ==
"this is a helper-defined translated exception"
135 with pytest.raises(m.MyException5)
as excinfo:
137 assert msg(excinfo.value) ==
"MyException5 subclass"
138 assert isinstance(excinfo.value, m.MyException5_1)
140 with pytest.raises(m.MyException5_1)
as excinfo:
142 assert msg(excinfo.value) ==
"MyException5 subclass"
144 with pytest.raises(m.MyException5)
as excinfo:
147 except m.MyException5_1:
148 raise RuntimeError(
"Exception error: caught child from parent")
149 assert msg(excinfo.value) ==
"this is a helper-defined translated exception"
153 """Tests nested (e.g. C++ -> Python -> C++) exception handling"""
156 raise m.MyException(
"nested error")
159 raise m.MyException5(
"nested error 5")
165 m.try_catch(m.MyException5, throw_myex5)
166 assert str(capture).startswith(
"MyException5: nested error 5")
169 with pytest.raises(m.MyException)
as excinfo:
170 m.try_catch(m.MyException5, throw_myex)
171 assert str(excinfo.value) ==
"nested error"
173 def pycatch(exctype, f, *args):
176 except m.MyException
as e:
189 assert str(capture).startswith(
"MyException5: nested error 5")
193 m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4)
194 assert capture ==
"this error is rethrown"
197 with pytest.raises(m.MyException5)
as excinfo:
198 m.try_catch(m.MyException, pycatch, m.MyException, m.throws5)
199 assert str(excinfo.value) ==
"this is a helper-defined translated exception"
206 raise AttributeError(
"Example error")
208 with pytest.raises(TypeError):
209 m.simple_bool_passthrough(MyRepr())