From f07d5069d86e31ecf195664850f79fb00c445bd3 Mon Sep 17 00:00:00 2001 From: Jason Lowe-Power Date: Fri, 17 Nov 2017 17:02:05 -0800 Subject: ext: Upgrade PyBind11 to version 2.2.1 This upgrade is necessary for pybind to build with GCC 7.2. We still need to add the patch for stl.h. MSC_FULL_VER change is no longer needed. See https://gem5-review.googlesource.com/c/public/gem5/+/2230 Change-Id: I806729217d022070583994c2dfcaa74476aef30f Signed-off-by: Jason Lowe-Power Reviewed-on: https://gem5-review.googlesource.com/5801 Reviewed-by: Andreas Sandberg Maintainer: Andreas Sandberg --- ext/pybind11/tests/test_exceptions.cpp | 69 ++++++++++++++++------------------ 1 file changed, 32 insertions(+), 37 deletions(-) (limited to 'ext/pybind11/tests/test_exceptions.cpp') 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 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(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; + } + }); + +} -- cgit v1.2.3