MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_custom

Function test_custom

tests/test_exceptions.py:138–202  ·  view source on GitHub ↗
(msg)

Source from the content-addressed store, hash-verified

136
137
138def test_custom(msg):
139 # Can we catch a MyException?
140 with pytest.raises(m.MyException) as excinfo:
141 m.throws1()
142 assert msg(excinfo.value) == "this error should go to py::exception<MyException>"
143
144 # Can we catch a MyExceptionUseDeprecatedOperatorCall?
145 with pytest.raises(m.MyExceptionUseDeprecatedOperatorCall) as excinfo:
146 m.throws1d()
147 assert (
148 msg(excinfo.value)
149 == "this error should go to py::exception<MyExceptionUseDeprecatedOperatorCall>"
150 )
151
152 # Can we translate to standard Python exceptions?
153 with pytest.raises(RuntimeError) as excinfo:
154 m.throws2()
155 assert msg(excinfo.value) == "this error should go to a standard Python exception"
156
157 # Can we handle unknown exceptions?
158 with pytest.raises(RuntimeError) as excinfo:
159 m.throws3()
160 assert msg(excinfo.value) == "Caught an unknown exception!"
161
162 # Can we delegate to another handler by rethrowing?
163 with pytest.raises(m.MyException) as excinfo:
164 m.throws4()
165 assert msg(excinfo.value) == "this error is rethrown"
166
167 # Can we fall-through to the default handler?
168 with pytest.raises(RuntimeError) as excinfo:
169 m.throws_logic_error()
170 assert (
171 msg(excinfo.value) == "this error should fall through to the standard handler"
172 )
173
174 # OverFlow error translation.
175 with pytest.raises(OverflowError) as excinfo:
176 m.throws_overflow_error()
177
178 # Can we handle a helper-declared exception?
179 with pytest.raises(m.MyException5) as excinfo:
180 m.throws5()
181 assert msg(excinfo.value) == "this is a helper-defined translated exception"
182
183 # Exception subclassing:
184 with pytest.raises(m.MyException5) as excinfo:
185 m.throws5_1()
186 assert msg(excinfo.value) == "MyException5 subclass"
187 assert isinstance(excinfo.value, m.MyException5_1)
188
189 with pytest.raises(m.MyException5_1) as excinfo:
190 m.throws5_1()
191 assert msg(excinfo.value) == "MyException5 subclass"
192
193 with pytest.raises(m.MyException5) as excinfo: # noqa: PT012
194 try:
195 m.throws5()

Callers

nothing calls this directly

Calls 2

msgFunction · 0.85
isinstanceFunction · 0.85

Tested by

no test coverage detected