Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
| 3 | |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 4 | def 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 Smirnov | 67b5489 | 2016-09-07 21:10:16 +0100 | [diff] [blame] | 12 | def 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 Moldovan | 135ba8d | 2016-09-10 11:58:02 +0200 | [diff] [blame] | 24 | def 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 Miroshnychenko | 83a8a97 | 2017-04-02 23:38:50 +0300 | [diff] [blame] | 32 | def test_exception_matches(): |
| 33 | from pybind11_tests import exception_matches |
| 34 | exception_matches() |
| 35 | |
| 36 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 37 | def test_custom(msg): |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 38 | from pybind11_tests import (MyException, MyException5, MyException5_1, |
| 39 | throws1, throws2, throws3, throws4, throws5, throws5_1, |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 40 | 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 Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 66 | |
| 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 Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 85 | except MyException5_1: |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 86 | raise RuntimeError("Exception error: caught child from parent") |
| 87 | assert msg(excinfo.value) == "this is a helper-defined translated exception" |