ext: Update pybind11 to v2.8.1
Change-Id: Ia1c7081377f53fd470addf35526f8b28a949a7b0 Signed-off-by: Jason Lowe-Power <jason@lowepower.com> Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/52523 Maintainer: Bobby R. Bruce <bbruce@ucdavis.edu> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Gabe Black <gabe.black@gmail.com>
This commit is contained in:
committed by
Jason Lowe-Power
parent
ba5f68db3d
commit
1e8aeee698
@@ -6,8 +6,12 @@
|
||||
All rights reserved. Use of this source code is governed by a
|
||||
BSD-style license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include "test_exceptions.h"
|
||||
|
||||
#include "local_bindings.h"
|
||||
|
||||
#include "pybind11_tests.h"
|
||||
#include <utility>
|
||||
|
||||
// A type that should be raised as an exception in Python
|
||||
class MyException : public std::exception {
|
||||
@@ -65,8 +69,19 @@ class MyException5_1 : public MyException5 {
|
||||
using MyException5::MyException5;
|
||||
};
|
||||
|
||||
|
||||
// Exception that will be caught via the module local translator.
|
||||
class MyException6 : public std::exception {
|
||||
public:
|
||||
explicit MyException6(const char * m) : message{m} {}
|
||||
const char * what() const noexcept override {return message.c_str();}
|
||||
private:
|
||||
std::string message = "";
|
||||
};
|
||||
|
||||
|
||||
struct PythonCallInDestructor {
|
||||
PythonCallInDestructor(const py::dict &d) : d(d) {}
|
||||
explicit PythonCallInDestructor(const py::dict &d) : d(d) {}
|
||||
~PythonCallInDestructor() { d["good"] = true; }
|
||||
|
||||
py::dict d;
|
||||
@@ -75,7 +90,7 @@ struct PythonCallInDestructor {
|
||||
|
||||
|
||||
struct PythonAlreadySetInDestructor {
|
||||
PythonAlreadySetInDestructor(const py::str &s) : s(s) {}
|
||||
explicit PythonAlreadySetInDestructor(const py::str &s) : s(s) {}
|
||||
~PythonAlreadySetInDestructor() {
|
||||
py::dict foo;
|
||||
try {
|
||||
@@ -135,14 +150,29 @@ TEST_SUBMODULE(exceptions, 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());
|
||||
|
||||
//py::register_local_exception<LocalSimpleException>(m, "LocalSimpleException")
|
||||
|
||||
py::register_local_exception_translator([](std::exception_ptr p) {
|
||||
try {
|
||||
if (p) {
|
||||
std::rethrow_exception(p);
|
||||
}
|
||||
} catch (const MyException6 &e) {
|
||||
PyErr_SetString(PyExc_RuntimeError, e.what());
|
||||
}
|
||||
});
|
||||
|
||||
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("throws6", []() { throw MyException6("MyException6 only handled in this module"); });
|
||||
m.def("throws_logic_error", []() { throw std::logic_error("this error should fall through to the standard handler"); });
|
||||
m.def("throws_overflow_error", []() {throw std::overflow_error(""); });
|
||||
m.def("throws_overflow_error", []() { throw std::overflow_error(""); });
|
||||
m.def("throws_local_error", []() { throw LocalException("never caught"); });
|
||||
m.def("throws_local_simple_error", []() { throw LocalSimpleException("this mod"); });
|
||||
m.def("exception_matches", []() {
|
||||
py::dict foo;
|
||||
try {
|
||||
@@ -198,34 +228,58 @@ TEST_SUBMODULE(exceptions, m) {
|
||||
throw py::error_already_set();
|
||||
});
|
||||
|
||||
m.def("python_call_in_destructor", [](py::dict d) {
|
||||
m.def("python_call_in_destructor", [](const py::dict &d) {
|
||||
bool retval = false;
|
||||
try {
|
||||
PythonCallInDestructor set_dict_in_destructor(d);
|
||||
PyErr_SetString(PyExc_ValueError, "foo");
|
||||
throw py::error_already_set();
|
||||
} catch (const py::error_already_set&) {
|
||||
return true;
|
||||
retval = true;
|
||||
}
|
||||
return false;
|
||||
return retval;
|
||||
});
|
||||
|
||||
m.def("python_alreadyset_in_destructor", [](py::str s) {
|
||||
m.def("python_alreadyset_in_destructor", [](const py::str &s) {
|
||||
PythonAlreadySetInDestructor alreadyset_in_destructor(s);
|
||||
return true;
|
||||
});
|
||||
|
||||
// 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;
|
||||
}
|
||||
});
|
||||
m.def("try_catch",
|
||||
[m](const py::object &exc_type, const py::function &f, const py::args &args) {
|
||||
try {
|
||||
f(*args);
|
||||
} catch (py::error_already_set &ex) {
|
||||
if (ex.matches(exc_type))
|
||||
py::print(ex.what());
|
||||
else
|
||||
throw;
|
||||
}
|
||||
});
|
||||
|
||||
// Test repr that cannot be displayed
|
||||
m.def("simple_bool_passthrough", [](bool x) {return x;});
|
||||
|
||||
m.def("throw_should_be_translated_to_key_error", []() { throw shared_exception(); });
|
||||
|
||||
#if PY_VERSION_HEX >= 0x03030000
|
||||
|
||||
m.def("raise_from", []() {
|
||||
PyErr_SetString(PyExc_ValueError, "inner");
|
||||
py::raise_from(PyExc_ValueError, "outer");
|
||||
throw py::error_already_set();
|
||||
});
|
||||
|
||||
m.def("raise_from_already_set", []() {
|
||||
try {
|
||||
PyErr_SetString(PyExc_ValueError, "inner");
|
||||
throw py::error_already_set();
|
||||
} catch (py::error_already_set& e) {
|
||||
py::raise_from(e, PyExc_ValueError, "outer");
|
||||
throw py::error_already_set();
|
||||
}
|
||||
});
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user