diff options
Diffstat (limited to 'ext/pybind11/tests/test_exceptions.cpp')
-rw-r--r-- | ext/pybind11/tests/test_exceptions.cpp | 69 |
1 files changed, 32 insertions, 37 deletions
diff --git a/ext/pybind11/tests/test_exceptions.cpp b/ext/pybind11/tests/test_exceptions.cpp index 706b500f2..ae28abb48 100644 --- a/ext/pybind11/tests/test_exceptions.cpp +++ b/ext/pybind11/tests/test_exceptions.cpp @@ -58,34 +58,6 @@ class MyException5_1 : public MyException5 { using MyException5::MyException5; }; -void throws1() { - throw MyException("this error should go to a custom type"); -} - -void throws2() { - throw MyException2("this error should go to a standard Python exception"); -} - -void throws3() { - throw MyException3("this error cannot be translated"); -} - -void throws4() { - throw MyException4("this error is rethrown"); -} - -void throws5() { - throw MyException5("this is a helper-defined translated exception"); -} - -void throws5_1() { - throw MyException5_1("MyException5 subclass"); -} - -void throws_logic_error() { - throw std::logic_error("this error should fall through to the standard handler"); -} - struct PythonCallInDestructor { PythonCallInDestructor(const py::dict &d) : d(d) {} ~PythonCallInDestructor() { d["good"] = true; } @@ -93,7 +65,11 @@ struct PythonCallInDestructor { py::dict d; }; -test_initializer custom_exceptions([](py::module &m) { +TEST_SUBMODULE(exceptions, m) { + m.def("throw_std_exception", []() { + throw std::runtime_error("This exception was intentionally thrown."); + }); + // make a new custom exception and use it as a translation target static py::exception<MyException> ex(m, "MyException"); py::register_exception_translator([](std::exception_ptr p) { @@ -133,13 +109,20 @@ test_initializer custom_exceptions([](py::module &m) { // A slightly more complicated one that declares MyException5_1 as a subclass of MyException5 py::register_exception<MyException5_1>(m, "MyException5_1", ex5.ptr()); - m.def("throws1", &throws1); - m.def("throws2", &throws2); - m.def("throws3", &throws3); - m.def("throws4", &throws4); - m.def("throws5", &throws5); - m.def("throws5_1", &throws5_1); - m.def("throws_logic_error", &throws_logic_error); + m.def("throws1", []() { throw MyException("this error should go to a custom type"); }); + m.def("throws2", []() { throw MyException2("this error should go to a standard Python exception"); }); + m.def("throws3", []() { throw MyException3("this error cannot be translated"); }); + m.def("throws4", []() { throw MyException4("this error is rethrown"); }); + m.def("throws5", []() { throw MyException5("this is a helper-defined translated exception"); }); + m.def("throws5_1", []() { throw MyException5_1("MyException5 subclass"); }); + m.def("throws_logic_error", []() { throw std::logic_error("this error should fall through to the standard handler"); }); + m.def("exception_matches", []() { + py::dict foo; + try { foo["bar"]; } + catch (py::error_already_set& ex) { + if (!ex.matches(PyExc_KeyError)) throw; + } + }); m.def("throw_already_set", [](bool err) { if (err) @@ -170,4 +153,16 @@ test_initializer custom_exceptions([](py::module &m) { } return false; }); -}); + + // test_nested_throws + m.def("try_catch", [m](py::object exc_type, py::function f, py::args args) { + try { f(*args); } + catch (py::error_already_set &ex) { + if (ex.matches(exc_type)) + py::print(ex.what()); + else + throw; + } + }); + +} |