blob: cc4baaa0918e09f5ca5996254e73dc6a5144fce6 [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
2
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -04003from pybind11_tests import exceptions as m
4
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02005
Dean Moldovan83e328f2017-06-09 00:44:49 +02006def test_std_exception(msg):
Dean Moldovan83e328f2017-06-09 00:44:49 +02007 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -04008 m.throw_std_exception()
Dean Moldovan83e328f2017-06-09 00:44:49 +02009 assert msg(excinfo.value) == "This exception was intentionally thrown."
10
11
Ivan Smirnov67b54892016-09-07 21:10:16 +010012def test_error_already_set(msg):
Ivan Smirnov67b54892016-09-07 21:10:16 +010013 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040014 m.throw_already_set(False)
Ivan Smirnov67b54892016-09-07 21:10:16 +010015 assert msg(excinfo.value) == "Unknown internal error occurred"
16
17 with pytest.raises(ValueError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040018 m.throw_already_set(True)
Ivan Smirnov67b54892016-09-07 21:10:16 +010019 assert msg(excinfo.value) == "foo"
20
21
Dean Moldovan135ba8d2016-09-10 11:58:02 +020022def test_python_call_in_catch():
Dean Moldovan135ba8d2016-09-10 11:58:02 +020023 d = {}
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040024 assert m.python_call_in_destructor(d) is True
Dean Moldovan135ba8d2016-09-10 11:58:02 +020025 assert d["good"] is True
26
27
Roman Miroshnychenko83a8a972017-04-02 23:38:50 +030028def test_exception_matches():
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040029 m.exception_matches()
Roman Miroshnychenko83a8a972017-04-02 23:38:50 +030030
31
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020032def test_custom(msg):
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040033 # Can we catch a MyException?
34 with pytest.raises(m.MyException) as excinfo:
35 m.throws1()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020036 assert msg(excinfo.value) == "this error should go to a custom type"
37
38 # Can we translate to standard Python exceptions?
39 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040040 m.throws2()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020041 assert msg(excinfo.value) == "this error should go to a standard Python exception"
42
43 # Can we handle unknown exceptions?
44 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040045 m.throws3()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020046 assert msg(excinfo.value) == "Caught an unknown exception!"
47
48 # Can we delegate to another handler by rethrowing?
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040049 with pytest.raises(m.MyException) as excinfo:
50 m.throws4()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020051 assert msg(excinfo.value) == "this error is rethrown"
52
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040053 # Can we fall-through to the default handler?
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020054 with pytest.raises(RuntimeError) as excinfo:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040055 m.throws_logic_error()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020056 assert msg(excinfo.value) == "this error should fall through to the standard handler"
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040057
58 # Can we handle a helper-declared exception?
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040059 with pytest.raises(m.MyException5) as excinfo:
60 m.throws5()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040061 assert msg(excinfo.value) == "this is a helper-defined translated exception"
62
63 # Exception subclassing:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040064 with pytest.raises(m.MyException5) as excinfo:
65 m.throws5_1()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040066 assert msg(excinfo.value) == "MyException5 subclass"
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040067 assert isinstance(excinfo.value, m.MyException5_1)
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040068
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040069 with pytest.raises(m.MyException5_1) as excinfo:
70 m.throws5_1()
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040071 assert msg(excinfo.value) == "MyException5 subclass"
72
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040073 with pytest.raises(m.MyException5) as excinfo:
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040074 try:
Jason Rhinelanderabcf43d2017-07-23 12:26:17 -040075 m.throws5()
76 except m.MyException5_1:
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040077 raise RuntimeError("Exception error: caught child from parent")
78 assert msg(excinfo.value) == "this is a helper-defined translated exception"