Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 3 | from pybind11_tests import exceptions as m |
Jason Rhinelander | d598172 | 2017-07-28 21:38:23 -0400 | [diff] [blame] | 4 | import pybind11_cross_module_tests as cm |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 5 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 6 | |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 7 | def test_std_exception(msg): |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 8 | with pytest.raises(RuntimeError) as excinfo: |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 9 | m.throw_std_exception() |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 10 | assert msg(excinfo.value) == "This exception was intentionally thrown." |
| 11 | |
| 12 | |
Ivan Smirnov | 67b5489 | 2016-09-07 21:10:16 +0100 | [diff] [blame] | 13 | def test_error_already_set(msg): |
Ivan Smirnov | 67b5489 | 2016-09-07 21:10:16 +0100 | [diff] [blame] | 14 | with pytest.raises(RuntimeError) as excinfo: |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 15 | m.throw_already_set(False) |
Ivan Smirnov | 67b5489 | 2016-09-07 21:10:16 +0100 | [diff] [blame] | 16 | assert msg(excinfo.value) == "Unknown internal error occurred" |
| 17 | |
| 18 | with pytest.raises(ValueError) as excinfo: |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 19 | m.throw_already_set(True) |
Ivan Smirnov | 67b5489 | 2016-09-07 21:10:16 +0100 | [diff] [blame] | 20 | assert msg(excinfo.value) == "foo" |
| 21 | |
| 22 | |
Jason Rhinelander | d598172 | 2017-07-28 21:38:23 -0400 | [diff] [blame] | 23 | def test_cross_module_exceptions(): |
| 24 | with pytest.raises(RuntimeError) as excinfo: |
| 25 | cm.raise_runtime_error() |
| 26 | assert str(excinfo.value) == "My runtime error" |
| 27 | |
| 28 | with pytest.raises(ValueError) as excinfo: |
| 29 | cm.raise_value_error() |
| 30 | assert str(excinfo.value) == "My value error" |
| 31 | |
| 32 | with pytest.raises(ValueError) as excinfo: |
| 33 | cm.throw_pybind_value_error() |
| 34 | assert str(excinfo.value) == "pybind11 value error" |
| 35 | |
| 36 | with pytest.raises(TypeError) as excinfo: |
| 37 | cm.throw_pybind_type_error() |
| 38 | assert str(excinfo.value) == "pybind11 type error" |
| 39 | |
| 40 | with pytest.raises(StopIteration) as excinfo: |
| 41 | cm.throw_stop_iteration() |
| 42 | |
| 43 | |
Dean Moldovan | 135ba8d | 2016-09-10 11:58:02 +0200 | [diff] [blame] | 44 | def test_python_call_in_catch(): |
Dean Moldovan | 135ba8d | 2016-09-10 11:58:02 +0200 | [diff] [blame] | 45 | d = {} |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 46 | assert m.python_call_in_destructor(d) is True |
Dean Moldovan | 135ba8d | 2016-09-10 11:58:02 +0200 | [diff] [blame] | 47 | assert d["good"] is True |
| 48 | |
| 49 | |
Roman Miroshnychenko | 83a8a97 | 2017-04-02 23:38:50 +0300 | [diff] [blame] | 50 | def test_exception_matches(): |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 51 | m.exception_matches() |
Roman Miroshnychenko | 83a8a97 | 2017-04-02 23:38:50 +0300 | [diff] [blame] | 52 | |
| 53 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 54 | def test_custom(msg): |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 55 | # Can we catch a MyException? |
| 56 | with pytest.raises(m.MyException) as excinfo: |
| 57 | m.throws1() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 58 | assert msg(excinfo.value) == "this error should go to a custom type" |
| 59 | |
| 60 | # Can we translate to standard Python exceptions? |
| 61 | with pytest.raises(RuntimeError) as excinfo: |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 62 | m.throws2() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 63 | assert msg(excinfo.value) == "this error should go to a standard Python exception" |
| 64 | |
| 65 | # Can we handle unknown exceptions? |
| 66 | with pytest.raises(RuntimeError) as excinfo: |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 67 | m.throws3() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 68 | assert msg(excinfo.value) == "Caught an unknown exception!" |
| 69 | |
| 70 | # Can we delegate to another handler by rethrowing? |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 71 | with pytest.raises(m.MyException) as excinfo: |
| 72 | m.throws4() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 73 | assert msg(excinfo.value) == "this error is rethrown" |
| 74 | |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 75 | # Can we fall-through to the default handler? |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 76 | with pytest.raises(RuntimeError) as excinfo: |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 77 | m.throws_logic_error() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 78 | assert msg(excinfo.value) == "this error should fall through to the standard handler" |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 79 | |
| 80 | # Can we handle a helper-declared exception? |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 81 | with pytest.raises(m.MyException5) as excinfo: |
| 82 | m.throws5() |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 83 | assert msg(excinfo.value) == "this is a helper-defined translated exception" |
| 84 | |
| 85 | # Exception subclassing: |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 86 | with pytest.raises(m.MyException5) as excinfo: |
| 87 | m.throws5_1() |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 88 | assert msg(excinfo.value) == "MyException5 subclass" |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 89 | assert isinstance(excinfo.value, m.MyException5_1) |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 90 | |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 91 | with pytest.raises(m.MyException5_1) as excinfo: |
| 92 | m.throws5_1() |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 93 | assert msg(excinfo.value) == "MyException5 subclass" |
| 94 | |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 95 | with pytest.raises(m.MyException5) as excinfo: |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 96 | try: |
Jason Rhinelander | abcf43d | 2017-07-23 12:26:17 -0400 | [diff] [blame] | 97 | m.throws5() |
| 98 | except m.MyException5_1: |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 99 | raise RuntimeError("Exception error: caught child from parent") |
| 100 | assert msg(excinfo.value) == "this is a helper-defined translated exception" |
Jason Rhinelander | 1682b67 | 2017-07-20 23:14:33 -0400 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def test_nested_throws(capture): |
| 104 | """Tests nested (e.g. C++ -> Python -> C++) exception handling""" |
| 105 | |
| 106 | def throw_myex(): |
| 107 | raise m.MyException("nested error") |
| 108 | |
| 109 | def throw_myex5(): |
| 110 | raise m.MyException5("nested error 5") |
| 111 | |
| 112 | # In the comments below, the exception is caught in the first step, thrown in the last step |
| 113 | |
| 114 | # C++ -> Python |
| 115 | with capture: |
| 116 | m.try_catch(m.MyException5, throw_myex5) |
| 117 | assert str(capture).startswith("MyException5: nested error 5") |
| 118 | |
| 119 | # Python -> C++ -> Python |
| 120 | with pytest.raises(m.MyException) as excinfo: |
| 121 | m.try_catch(m.MyException5, throw_myex) |
| 122 | assert str(excinfo.value) == "nested error" |
| 123 | |
| 124 | def pycatch(exctype, f, *args): |
| 125 | try: |
| 126 | f(*args) |
| 127 | except m.MyException as e: |
| 128 | print(e) |
| 129 | |
| 130 | # C++ -> Python -> C++ -> Python |
| 131 | with capture: |
| 132 | m.try_catch( |
| 133 | m.MyException5, pycatch, m.MyException, m.try_catch, m.MyException, throw_myex5) |
| 134 | assert str(capture).startswith("MyException5: nested error 5") |
| 135 | |
| 136 | # C++ -> Python -> C++ |
| 137 | with capture: |
| 138 | m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4) |
| 139 | assert capture == "this error is rethrown" |
| 140 | |
| 141 | # Python -> C++ -> Python -> C++ |
| 142 | with pytest.raises(m.MyException5) as excinfo: |
| 143 | m.try_catch(m.MyException, pycatch, m.MyException, m.throws5) |
| 144 | assert str(excinfo.value) == "this is a helper-defined translated exception" |