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 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 24 | def test_custom(msg): |
| 25 | from pybind11_tests import (MyException, throws1, throws2, throws3, throws4, |
| 26 | throws_logic_error) |
| 27 | |
| 28 | # Can we catch a MyException?" |
| 29 | with pytest.raises(MyException) as excinfo: |
| 30 | throws1() |
| 31 | assert msg(excinfo.value) == "this error should go to a custom type" |
| 32 | |
| 33 | # Can we translate to standard Python exceptions? |
| 34 | with pytest.raises(RuntimeError) as excinfo: |
| 35 | throws2() |
| 36 | assert msg(excinfo.value) == "this error should go to a standard Python exception" |
| 37 | |
| 38 | # Can we handle unknown exceptions? |
| 39 | with pytest.raises(RuntimeError) as excinfo: |
| 40 | throws3() |
| 41 | assert msg(excinfo.value) == "Caught an unknown exception!" |
| 42 | |
| 43 | # Can we delegate to another handler by rethrowing? |
| 44 | with pytest.raises(MyException) as excinfo: |
| 45 | throws4() |
| 46 | assert msg(excinfo.value) == "this error is rethrown" |
| 47 | |
| 48 | # "Can we fall-through to the default handler?" |
| 49 | with pytest.raises(RuntimeError) as excinfo: |
| 50 | throws_logic_error() |
| 51 | assert msg(excinfo.value) == "this error should fall through to the standard handler" |