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