Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
| 3 | |
Ivan Smirnov | 67b5489 | 2016-09-07 21:10:16 +0100 | [diff] [blame] | 4 | def 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 Moldovan | 135ba8d | 2016-09-10 11:58:02 +0200 | [diff] [blame] | 16 | def 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 Miroshnychenko | 83a8a97 | 2017-04-02 23:38:50 +0300 | [diff] [blame] | 24 | def test_exception_matches(): |
| 25 | from pybind11_tests import exception_matches |
| 26 | exception_matches() |
| 27 | |
| 28 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 29 | def test_custom(msg): |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 30 | from pybind11_tests import (MyException, MyException5, MyException5_1, |
| 31 | throws1, throws2, throws3, throws4, throws5, throws5_1, |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 32 | 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 Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 58 | |
| 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 Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 77 | except MyException5_1: |
Jason Rhinelander | b3794f1 | 2016-09-16 02:04:15 -0400 | [diff] [blame] | 78 | raise RuntimeError("Exception error: caught child from parent") |
| 79 | assert msg(excinfo.value) == "this is a helper-defined translated exception" |