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 57 if hasattr(sys,
'unraisablehook'):
59 default_hook = sys.unraisablehook
61 def hook(unraisable_hook_args):
62 exc_type, exc_value, exc_tb, err_msg, obj = unraisable_hook_args
63 if obj ==
'already_set demo':
65 default_hook(unraisable_hook_args)
69 monkeypatch.setattr(sys,
'unraisablehook', hook)
71 assert m.python_alreadyset_in_destructor(
'already_set demo')
is True 73 assert triggered[0]
is True 75 _, captured_stderr = capsys.readouterr()
77 assert 'ignored' in captured_stderr
and 'already_set demo' in captured_stderr
81 assert m.exception_matches()
82 assert m.exception_matches_base()
83 assert m.modulenotfound_exception_matches_base()
88 with pytest.raises(m.MyException)
as excinfo:
90 assert msg(excinfo.value) ==
"this error should go to a custom type" 93 with pytest.raises(RuntimeError)
as excinfo:
95 assert msg(excinfo.value) ==
"this error should go to a standard Python exception" 98 with pytest.raises(RuntimeError)
as excinfo:
100 assert msg(excinfo.value) ==
"Caught an unknown exception!" 103 with pytest.raises(m.MyException)
as excinfo:
105 assert msg(excinfo.value) ==
"this error is rethrown" 108 with pytest.raises(RuntimeError)
as excinfo:
109 m.throws_logic_error()
110 assert msg(excinfo.value) ==
"this error should fall through to the standard handler" 113 with pytest.raises(OverflowError)
as excinfo:
114 m.throws_overflow_error()
117 with pytest.raises(m.MyException5)
as excinfo:
119 assert msg(excinfo.value) ==
"this is a helper-defined translated exception" 122 with pytest.raises(m.MyException5)
as excinfo:
124 assert msg(excinfo.value) ==
"MyException5 subclass" 125 assert isinstance(excinfo.value, m.MyException5_1)
127 with pytest.raises(m.MyException5_1)
as excinfo:
129 assert msg(excinfo.value) ==
"MyException5 subclass" 131 with pytest.raises(m.MyException5)
as excinfo:
134 except m.MyException5_1:
135 raise RuntimeError(
"Exception error: caught child from parent")
136 assert msg(excinfo.value) ==
"this is a helper-defined translated exception" 140 """Tests nested (e.g. C++ -> Python -> C++) exception handling""" 143 raise m.MyException(
"nested error")
146 raise m.MyException5(
"nested error 5")
152 m.try_catch(m.MyException5, throw_myex5)
153 assert str(capture).startswith(
"MyException5: nested error 5")
156 with pytest.raises(m.MyException)
as excinfo:
157 m.try_catch(m.MyException5, throw_myex)
158 assert str(excinfo.value) ==
"nested error" 160 def pycatch(exctype, f, *args):
163 except m.MyException
as e:
169 m.MyException5, pycatch, m.MyException, m.try_catch, m.MyException, throw_myex5)
170 assert str(capture).startswith(
"MyException5: nested error 5")
174 m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4)
175 assert capture ==
"this error is rethrown" 178 with pytest.raises(m.MyException5)
as excinfo:
179 m.try_catch(m.MyException, pycatch, m.MyException, m.throws5)
180 assert str(excinfo.value) ==
"this is a helper-defined translated exception" 188 raise AttributeError(
"Example error")
190 with pytest.raises(TypeError):
191 m.simple_bool_passthrough(MyRepr())
void print(const Matrix &A, const string &s, ostream &stream)
def test_std_exception(msg)
bool hasattr(handle obj, handle name)
def test_exception_matches()
def test_cross_module_exceptions()
def test_error_already_set(msg)
bool isinstance(handle obj)
def test_python_alreadyset_in_destructor(monkeypatch, capsys)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
def test_python_call_in_catch()
def test_nested_throws(capture)