blob: 053e7d4a288c3f192f0b3e441adec33ee5c75298 [file] [log] [blame]
Henry Schreinerd8c7ee02020-07-20 13:35:21 -04001# -*- coding: utf-8 -*-
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002import pytest
3
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -04004from pybind11_tests import exceptions as m
Jason Rhinelanderd5981722017-07-28 21:38:23 -04005import pybind11_cross_module_tests as cm
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -04006
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02007
Dean Moldovan83e328f2017-06-09 00:44:49 +02008def test_std_exception(msg):
Dean Moldovan83e328f2017-06-09 00:44:49 +02009 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040010 m.throw_std_exception()
Dean Moldovan83e328f2017-06-09 00:44:49 +020011 assert msg(excinfo.value) == "This exception was intentionally thrown."
12
13
Ivan Smirnov67b54892016-09-07 21:10:16 +010014def test_error_already_set(msg):
Ivan Smirnov67b54892016-09-07 21:10:16 +010015 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040016 m.throw_already_set(False)
Ivan Smirnov67b54892016-09-07 21:10:16 +010017 assert msg(excinfo.value) == "Unknown internal error occurred"
18
19 with pytest.raises(ValueError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040020 m.throw_already_set(True)
Ivan Smirnov67b54892016-09-07 21:10:16 +010021 assert msg(excinfo.value) == "foo"
22
23
Jason Rhinelanderd5981722017-07-28 21:38:23 -040024def test_cross_module_exceptions():
25 with pytest.raises(RuntimeError) as excinfo:
26 cm.raise_runtime_error()
27 assert str(excinfo.value) == "My runtime error"
28
29 with pytest.raises(ValueError) as excinfo:
30 cm.raise_value_error()
31 assert str(excinfo.value) == "My value error"
32
33 with pytest.raises(ValueError) as excinfo:
34 cm.throw_pybind_value_error()
35 assert str(excinfo.value) == "pybind11 value error"
36
37 with pytest.raises(TypeError) as excinfo:
38 cm.throw_pybind_type_error()
39 assert str(excinfo.value) == "pybind11 type error"
40
41 with pytest.raises(StopIteration) as excinfo:
42 cm.throw_stop_iteration()
43
44
Dean Moldovan135ba8d2016-09-10 11:58:02 +020045def test_python_call_in_catch():
Dean Moldovan135ba8d2016-09-10 11:58:02 +020046 d = {}
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040047 assert m.python_call_in_destructor(d) is True
Dean Moldovan135ba8d2016-09-10 11:58:02 +020048 assert d["good"] is True
49
50
Roman Miroshnychenko83a8a972017-04-02 23:38:50 +030051def test_exception_matches():
Yannick Jadoul97784da2019-05-12 23:35:49 +020052 assert m.exception_matches()
53 assert m.exception_matches_base()
54 assert m.modulenotfound_exception_matches_base()
Roman Miroshnychenko83a8a972017-04-02 23:38:50 +030055
56
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020057def test_custom(msg):
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040058 # Can we catch a MyException?
59 with pytest.raises(m.MyException) as excinfo:
60 m.throws1()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020061 assert msg(excinfo.value) == "this error should go to a custom type"
62
63 # Can we translate to standard Python exceptions?
64 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040065 m.throws2()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020066 assert msg(excinfo.value) == "this error should go to a standard Python exception"
67
68 # Can we handle unknown exceptions?
69 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040070 m.throws3()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020071 assert msg(excinfo.value) == "Caught an unknown exception!"
72
73 # Can we delegate to another handler by rethrowing?
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040074 with pytest.raises(m.MyException) as excinfo:
75 m.throws4()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020076 assert msg(excinfo.value) == "this error is rethrown"
77
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040078 # Can we fall-through to the default handler?
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020079 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040080 m.throws_logic_error()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020081 assert msg(excinfo.value) == "this error should fall through to the standard handler"
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040082
Francesco Biscanideb3cb22019-11-14 08:56:58 +010083 # OverFlow error translation.
84 with pytest.raises(OverflowError) as excinfo:
85 m.throws_overflow_error()
86
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040087 # Can we handle a helper-declared exception?
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040088 with pytest.raises(m.MyException5) as excinfo:
89 m.throws5()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040090 assert msg(excinfo.value) == "this is a helper-defined translated exception"
91
92 # Exception subclassing:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040093 with pytest.raises(m.MyException5) as excinfo:
94 m.throws5_1()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040095 assert msg(excinfo.value) == "MyException5 subclass"
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040096 assert isinstance(excinfo.value, m.MyException5_1)
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040097
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040098 with pytest.raises(m.MyException5_1) as excinfo:
99 m.throws5_1()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400100 assert msg(excinfo.value) == "MyException5 subclass"
101
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -0400102 with pytest.raises(m.MyException5) as excinfo:
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400103 try:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -0400104 m.throws5()
105 except m.MyException5_1:
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400106 raise RuntimeError("Exception error: caught child from parent")
107 assert msg(excinfo.value) == "this is a helper-defined translated exception"
Jason Rhinelander1682b672017-07-20 23:14:33 -0400108
109
110def test_nested_throws(capture):
111 """Tests nested (e.g. C++ -> Python -> C++) exception handling"""
112
113 def throw_myex():
114 raise m.MyException("nested error")
115
116 def throw_myex5():
117 raise m.MyException5("nested error 5")
118
119 # In the comments below, the exception is caught in the first step, thrown in the last step
120
121 # C++ -> Python
122 with capture:
123 m.try_catch(m.MyException5, throw_myex5)
124 assert str(capture).startswith("MyException5: nested error 5")
125
126 # Python -> C++ -> Python
127 with pytest.raises(m.MyException) as excinfo:
128 m.try_catch(m.MyException5, throw_myex)
129 assert str(excinfo.value) == "nested error"
130
131 def pycatch(exctype, f, *args):
132 try:
133 f(*args)
134 except m.MyException as e:
135 print(e)
136
137 # C++ -> Python -> C++ -> Python
138 with capture:
139 m.try_catch(
140 m.MyException5, pycatch, m.MyException, m.try_catch, m.MyException, throw_myex5)
141 assert str(capture).startswith("MyException5: nested error 5")
142
143 # C++ -> Python -> C++
144 with capture:
145 m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4)
146 assert capture == "this error is rethrown"
147
148 # Python -> C++ -> Python -> C++
149 with pytest.raises(m.MyException5) as excinfo:
150 m.try_catch(m.MyException, pycatch, m.MyException, m.throws5)
151 assert str(excinfo.value) == "this is a helper-defined translated exception"