test_exceptions.py 4.59 KB
Newer Older
Dean Moldovan's avatar
Dean Moldovan committed
1
2
import pytest

3
from pybind11_tests import exceptions as m
4
import pybind11_cross_module_tests as cm
Dean Moldovan's avatar
Dean Moldovan committed
5

6

7
def test_std_exception(msg):
8
    with pytest.raises(RuntimeError) as excinfo:
9
        m.throw_std_exception()
10
11
12
    assert msg(excinfo.value) == "This exception was intentionally thrown."


13
14
def test_error_already_set(msg):
    with pytest.raises(RuntimeError) as excinfo:
15
        m.throw_already_set(False)
16
17
18
    assert msg(excinfo.value) == "Unknown internal error occurred"

    with pytest.raises(ValueError) as excinfo:
19
        m.throw_already_set(True)
20
21
22
    assert msg(excinfo.value) == "foo"


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def test_cross_module_exceptions():
    with pytest.raises(RuntimeError) as excinfo:
        cm.raise_runtime_error()
    assert str(excinfo.value) == "My runtime error"

    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()


44
45
def test_python_call_in_catch():
    d = {}
46
    assert m.python_call_in_destructor(d) is True
47
48
49
    assert d["good"] is True


50
def test_exception_matches():
51
    m.exception_matches()
52
53


Dean Moldovan's avatar
Dean Moldovan committed
54
def test_custom(msg):
55
56
57
    # Can we catch a MyException?
    with pytest.raises(m.MyException) as excinfo:
        m.throws1()
Dean Moldovan's avatar
Dean Moldovan committed
58
59
60
61
    assert msg(excinfo.value) == "this error should go to a custom type"

    # Can we translate to standard Python exceptions?
    with pytest.raises(RuntimeError) as excinfo:
62
        m.throws2()
Dean Moldovan's avatar
Dean Moldovan committed
63
64
65
66
    assert msg(excinfo.value) == "this error should go to a standard Python exception"

    # Can we handle unknown exceptions?
    with pytest.raises(RuntimeError) as excinfo:
67
        m.throws3()
Dean Moldovan's avatar
Dean Moldovan committed
68
69
70
    assert msg(excinfo.value) == "Caught an unknown exception!"

    # Can we delegate to another handler by rethrowing?
71
72
    with pytest.raises(m.MyException) as excinfo:
        m.throws4()
Dean Moldovan's avatar
Dean Moldovan committed
73
74
    assert msg(excinfo.value) == "this error is rethrown"

75
    # Can we fall-through to the default handler?
Dean Moldovan's avatar
Dean Moldovan committed
76
    with pytest.raises(RuntimeError) as excinfo:
77
        m.throws_logic_error()
Dean Moldovan's avatar
Dean Moldovan committed
78
    assert msg(excinfo.value) == "this error should fall through to the standard handler"
79
80

    # Can we handle a helper-declared exception?
81
82
    with pytest.raises(m.MyException5) as excinfo:
        m.throws5()
83
84
85
    assert msg(excinfo.value) == "this is a helper-defined translated exception"

    # Exception subclassing:
86
87
    with pytest.raises(m.MyException5) as excinfo:
        m.throws5_1()
88
    assert msg(excinfo.value) == "MyException5 subclass"
89
    assert isinstance(excinfo.value, m.MyException5_1)
90

91
92
    with pytest.raises(m.MyException5_1) as excinfo:
        m.throws5_1()
93
94
    assert msg(excinfo.value) == "MyException5 subclass"

95
    with pytest.raises(m.MyException5) as excinfo:
96
        try:
97
98
            m.throws5()
        except m.MyException5_1:
99
100
            raise RuntimeError("Exception error: caught child from parent")
    assert msg(excinfo.value) == "this is a helper-defined translated exception"
Jason Rhinelander's avatar
Jason Rhinelander committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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"