2a3,4
> from pybind11_tests import exceptions as m
> import pybind11_cross_module_tests as cm
4,5d5
< def test_error_already_set(msg):
< from pybind11_tests import throw_already_set
6a7
> def test_std_exception(msg):
8c9,15
< throw_already_set(False)
---
> m.throw_std_exception()
> assert msg(excinfo.value) == "This exception was intentionally thrown."
>
>
> def test_error_already_set(msg):
> with pytest.raises(RuntimeError) as excinfo:
> m.throw_already_set(False)
12c19
< throw_already_set(True)
---
> m.throw_already_set(True)
16,17c23,26
< def test_python_call_in_catch():
< from pybind11_tests import python_call_in_destructor
---
> def test_cross_module_exceptions():
> with pytest.raises(RuntimeError) as excinfo:
> cm.raise_runtime_error()
> assert str(excinfo.value) == "My runtime error"
18a28,44
> with pytest.raises(ValueError) as excinfo:
> cm.raise_value_error()
> assert str(excinfo.value) == "My value error"
>
> with pytest.raises(ValueError) as excinfo:
> cm.throw_pybind_value_error()
> assert str(excinfo.value) == "pybind11 value error"
>
> with pytest.raises(TypeError) as excinfo:
> cm.throw_pybind_type_error()
> assert str(excinfo.value) == "pybind11 type error"
>
> with pytest.raises(StopIteration) as excinfo:
> cm.throw_stop_iteration()
>
>
> def test_python_call_in_catch():
20c46
< assert python_call_in_destructor(d) is True
---
> assert m.python_call_in_destructor(d) is True
24,27c50,51
< def test_custom(msg):
< from pybind11_tests import (MyException, MyException5, MyException5_1,
< throws1, throws2, throws3, throws4, throws5, throws5_1,
< throws_logic_error)
---
> def test_exception_matches():
> m.exception_matches()
29,31c53,57
< # Can we catch a MyException?"
< with pytest.raises(MyException) as excinfo:
< throws1()
---
>
> def test_custom(msg):
> # Can we catch a MyException?
> with pytest.raises(m.MyException) as excinfo:
> m.throws1()
36c62
< throws2()
---
> m.throws2()
41c67
< throws3()
---
> m.throws3()
45,46c71,72
< with pytest.raises(MyException) as excinfo:
< throws4()
---
> with pytest.raises(m.MyException) as excinfo:
> m.throws4()
49c75
< # "Can we fall-through to the default handler?"
---
> # Can we fall-through to the default handler?
51c77
< throws_logic_error()
---
> m.throws_logic_error()
55,56c81,82
< with pytest.raises(MyException5) as excinfo:
< throws5()
---
> with pytest.raises(m.MyException5) as excinfo:
> m.throws5()
60,61c86,87
< with pytest.raises(MyException5) as excinfo:
< throws5_1()
---
> with pytest.raises(m.MyException5) as excinfo:
> m.throws5_1()
63c89
< assert isinstance(excinfo.value, MyException5_1)
---
> assert isinstance(excinfo.value, m.MyException5_1)
65,66c91,92
< with pytest.raises(MyException5_1) as excinfo:
< throws5_1()
---
> with pytest.raises(m.MyException5_1) as excinfo:
> m.throws5_1()
69c95
< with pytest.raises(MyException5) as excinfo:
---
> with pytest.raises(m.MyException5) as excinfo:
71,72c97,98
< throws5()
< except MyException5_1:
---
> m.throws5()
> except m.MyException5_1:
74a101,144
>
>
> def test_nested_throws(capture):
> """Tests nested (e.g. C++ -> Python -> C++) exception handling"""
>
> def throw_myex():
> raise m.MyException("nested error")
>
> def throw_myex5():
> raise m.MyException5("nested error 5")
>
> # In the comments below, the exception is caught in the first step, thrown in the last step
>
> # C++ -> Python
> with capture:
> m.try_catch(m.MyException5, throw_myex5)
> assert str(capture).startswith("MyException5: nested error 5")
>
> # Python -> C++ -> Python
> with pytest.raises(m.MyException) as excinfo:
> m.try_catch(m.MyException5, throw_myex)
> assert str(excinfo.value) == "nested error"
>
> def pycatch(exctype, f, *args):
> try:
> f(*args)
> except m.MyException as e:
> print(e)
>
> # C++ -> Python -> C++ -> Python
> with capture:
> m.try_catch(
> m.MyException5, pycatch, m.MyException, m.try_catch, m.MyException, throw_myex5)
> assert str(capture).startswith("MyException5: nested error 5")
>
> # C++ -> Python -> C++
> with capture:
> m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4)
> assert capture == "this error is rethrown"
>
> # Python -> C++ -> Python -> C++
> with pytest.raises(m.MyException5) as excinfo:
> m.try_catch(m.MyException, pycatch, m.MyException, m.throws5)
> assert str(excinfo.value) == "this is a helper-defined translated exception"