blob: 15d47876fff759f656b2a17bfec27f38763ead68 [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
2
3
Dean Moldovan83e328f2017-06-09 00:44:49 +02004def test_std_exception(msg):
5 from pybind11_tests import throw_std_exception
6
7 with pytest.raises(RuntimeError) as excinfo:
8 throw_std_exception()
9 assert msg(excinfo.value) == "This exception was intentionally thrown."
10
11
Ivan Smirnov67b54892016-09-07 21:10:16 +010012def test_error_already_set(msg):
13 from pybind11_tests import throw_already_set
14
15 with pytest.raises(RuntimeError) as excinfo:
16 throw_already_set(False)
17 assert msg(excinfo.value) == "Unknown internal error occurred"
18
19 with pytest.raises(ValueError) as excinfo:
20 throw_already_set(True)
21 assert msg(excinfo.value) == "foo"
22
23
Dean Moldovan135ba8d2016-09-10 11:58:02 +020024def test_python_call_in_catch():
25 from pybind11_tests import python_call_in_destructor
26
27 d = {}
28 assert python_call_in_destructor(d) is True
29 assert d["good"] is True
30
31
Roman Miroshnychenko83a8a972017-04-02 23:38:50 +030032def test_exception_matches():
33 from pybind11_tests import exception_matches
34 exception_matches()
35
36
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020037def test_custom(msg):
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040038 from pybind11_tests import (MyException, MyException5, MyException5_1,
39 throws1, throws2, throws3, throws4, throws5, throws5_1,
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020040 throws_logic_error)
41
42 # Can we catch a MyException?"
43 with pytest.raises(MyException) as excinfo:
44 throws1()
45 assert msg(excinfo.value) == "this error should go to a custom type"
46
47 # Can we translate to standard Python exceptions?
48 with pytest.raises(RuntimeError) as excinfo:
49 throws2()
50 assert msg(excinfo.value) == "this error should go to a standard Python exception"
51
52 # Can we handle unknown exceptions?
53 with pytest.raises(RuntimeError) as excinfo:
54 throws3()
55 assert msg(excinfo.value) == "Caught an unknown exception!"
56
57 # Can we delegate to another handler by rethrowing?
58 with pytest.raises(MyException) as excinfo:
59 throws4()
60 assert msg(excinfo.value) == "this error is rethrown"
61
62 # "Can we fall-through to the default handler?"
63 with pytest.raises(RuntimeError) as excinfo:
64 throws_logic_error()
65 assert msg(excinfo.value) == "this error should fall through to the standard handler"
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040066
67 # Can we handle a helper-declared exception?
68 with pytest.raises(MyException5) as excinfo:
69 throws5()
70 assert msg(excinfo.value) == "this is a helper-defined translated exception"
71
72 # Exception subclassing:
73 with pytest.raises(MyException5) as excinfo:
74 throws5_1()
75 assert msg(excinfo.value) == "MyException5 subclass"
76 assert isinstance(excinfo.value, MyException5_1)
77
78 with pytest.raises(MyException5_1) as excinfo:
79 throws5_1()
80 assert msg(excinfo.value) == "MyException5 subclass"
81
82 with pytest.raises(MyException5) as excinfo:
83 try:
84 throws5()
Dean Moldovanbad17402016-11-20 21:21:54 +010085 except MyException5_1:
Jason Rhinelanderb3794f12016-09-16 02:04:15 -040086 raise RuntimeError("Exception error: caught child from parent")
87 assert msg(excinfo.value) == "this is a helper-defined translated exception"