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