blob: 7d7088d00b8fec6aeab23f02c2646e3254b53917 [file] [log] [blame]
Henry Schreinerd8c7ee02020-07-20 13:35:21 -04001# -*- coding: utf-8 -*-
James R. Barlow3618bea2020-08-08 03:07:14 -07002import sys
3
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02004import pytest
5
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -04006from pybind11_tests import exceptions as m
Jason Rhinelanderd5981722017-07-28 21:38:23 -04007import pybind11_cross_module_tests as cm
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -04008
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02009
Dean Moldovan83e328f2017-06-09 00:44:49 +020010def test_std_exception(msg):
Dean Moldovan83e328f2017-06-09 00:44:49 +020011 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040012 m.throw_std_exception()
Dean Moldovan83e328f2017-06-09 00:44:49 +020013 assert msg(excinfo.value) == "This exception was intentionally thrown."
14
15
Ivan Smirnov67b54892016-09-07 21:10:16 +010016def test_error_already_set(msg):
Ivan Smirnov67b54892016-09-07 21:10:16 +010017 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040018 m.throw_already_set(False)
Ivan Smirnov67b54892016-09-07 21:10:16 +010019 assert msg(excinfo.value) == "Unknown internal error occurred"
20
21 with pytest.raises(ValueError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040022 m.throw_already_set(True)
Ivan Smirnov67b54892016-09-07 21:10:16 +010023 assert msg(excinfo.value) == "foo"
24
25
Jason Rhinelanderd5981722017-07-28 21:38:23 -040026def test_cross_module_exceptions():
27 with pytest.raises(RuntimeError) as excinfo:
28 cm.raise_runtime_error()
29 assert str(excinfo.value) == "My runtime error"
30
31 with pytest.raises(ValueError) as excinfo:
32 cm.raise_value_error()
33 assert str(excinfo.value) == "My value error"
34
35 with pytest.raises(ValueError) as excinfo:
36 cm.throw_pybind_value_error()
37 assert str(excinfo.value) == "pybind11 value error"
38
39 with pytest.raises(TypeError) as excinfo:
40 cm.throw_pybind_type_error()
41 assert str(excinfo.value) == "pybind11 type error"
42
43 with pytest.raises(StopIteration) as excinfo:
44 cm.throw_stop_iteration()
45
46
Dean Moldovan135ba8d2016-09-10 11:58:02 +020047def test_python_call_in_catch():
Dean Moldovan135ba8d2016-09-10 11:58:02 +020048 d = {}
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040049 assert m.python_call_in_destructor(d) is True
Dean Moldovan135ba8d2016-09-10 11:58:02 +020050 assert d["good"] is True
51
52
James R. Barlow3618bea2020-08-08 03:07:14 -070053def test_python_alreadyset_in_destructor(monkeypatch, capsys):
54 hooked = False
55 triggered = [False] # mutable, so Python 2.7 closure can modify it
56
57 if hasattr(sys, 'unraisablehook'): # Python 3.8+
58 hooked = True
59 default_hook = sys.unraisablehook
60
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':
64 triggered[0] = True
65 default_hook(unraisable_hook_args)
66 return
67
68 # Use monkeypatch so pytest can apply and remove the patch as appropriate
69 monkeypatch.setattr(sys, 'unraisablehook', hook)
70
71 assert m.python_alreadyset_in_destructor('already_set demo') is True
72 if hooked:
73 assert triggered[0] is True
74
75 _, captured_stderr = capsys.readouterr()
76 # Error message is different in Python 2 and 3, check for words that appear in both
77 assert 'ignored' in captured_stderr and 'already_set demo' in captured_stderr
78
79
Roman Miroshnychenko83a8a972017-04-02 23:38:50 +030080def test_exception_matches():
Yannick Jadoul97784da2019-05-12 23:35:49 +020081 assert m.exception_matches()
82 assert m.exception_matches_base()
83 assert m.modulenotfound_exception_matches_base()
Roman Miroshnychenko83a8a972017-04-02 23:38:50 +030084
85
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020086def test_custom(msg):
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040087 # Can we catch a MyException?
88 with pytest.raises(m.MyException) as excinfo:
89 m.throws1()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020090 assert msg(excinfo.value) == "this error should go to a custom type"
91
92 # Can we translate to standard Python exceptions?
93 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040094 m.throws2()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020095 assert msg(excinfo.value) == "this error should go to a standard Python exception"
96
97 # Can we handle unknown exceptions?
98 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040099 m.throws3()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200100 assert msg(excinfo.value) == "Caught an unknown exception!"
101
102 # Can we delegate to another handler by rethrowing?
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -0400103 with pytest.raises(m.MyException) as excinfo:
104 m.throws4()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200105 assert msg(excinfo.value) == "this error is rethrown"
106
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -0400107 # Can we fall-through to the default handler?
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200108 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -0400109 m.throws_logic_error()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200110 assert msg(excinfo.value) == "this error should fall through to the standard handler"
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400111
Francesco Biscanideb3cb22019-11-14 08:56:58 +0100112 # OverFlow error translation.
113 with pytest.raises(OverflowError) as excinfo:
114 m.throws_overflow_error()
115
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400116 # Can we handle a helper-declared exception?
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -0400117 with pytest.raises(m.MyException5) as excinfo:
118 m.throws5()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400119 assert msg(excinfo.value) == "this is a helper-defined translated exception"
120
121 # Exception subclassing:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -0400122 with pytest.raises(m.MyException5) as excinfo:
123 m.throws5_1()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400124 assert msg(excinfo.value) == "MyException5 subclass"
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -0400125 assert isinstance(excinfo.value, m.MyException5_1)
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400126
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -0400127 with pytest.raises(m.MyException5_1) as excinfo:
128 m.throws5_1()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400129 assert msg(excinfo.value) == "MyException5 subclass"
130
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -0400131 with pytest.raises(m.MyException5) as excinfo:
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400132 try:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -0400133 m.throws5()
134 except m.MyException5_1:
Jason Rhinelanderb3794f12016-09-16 02:04:15 -0400135 raise RuntimeError("Exception error: caught child from parent")
136 assert msg(excinfo.value) == "this is a helper-defined translated exception"
Jason Rhinelander1682b672017-07-20 23:14:33 -0400137
138
139def test_nested_throws(capture):
140 """Tests nested (e.g. C++ -> Python -> C++) exception handling"""
141
142 def throw_myex():
143 raise m.MyException("nested error")
144
145 def throw_myex5():
146 raise m.MyException5("nested error 5")
147
148 # In the comments below, the exception is caught in the first step, thrown in the last step
149
150 # C++ -> Python
151 with capture:
152 m.try_catch(m.MyException5, throw_myex5)
153 assert str(capture).startswith("MyException5: nested error 5")
154
155 # Python -> C++ -> Python
156 with pytest.raises(m.MyException) as excinfo:
157 m.try_catch(m.MyException5, throw_myex)
158 assert str(excinfo.value) == "nested error"
159
160 def pycatch(exctype, f, *args):
161 try:
162 f(*args)
163 except m.MyException as e:
164 print(e)
165
166 # C++ -> Python -> C++ -> Python
167 with capture:
168 m.try_catch(
169 m.MyException5, pycatch, m.MyException, m.try_catch, m.MyException, throw_myex5)
170 assert str(capture).startswith("MyException5: nested error 5")
171
172 # C++ -> Python -> C++
173 with capture:
174 m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4)
175 assert capture == "this error is rethrown"
176
177 # Python -> C++ -> Python -> C++
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"
Henry Schreinercf0a6452020-08-18 07:14:34 -0400181
182
183# This can often happen if you wrap a pybind11 class in a Python wrapper
184def test_invalid_repr():
185
186 class MyRepr(object):
187 def __repr__(self):
188 raise AttributeError("Example error")
189
190 with pytest.raises(TypeError):
191 m.simple_bool_passthrough(MyRepr())