mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-19 22:39:09 +00:00
[BUGFIX] Fixing pybind11::error_already_set.matches to also work with exception subclasses (#1715)
* Fixing order of arguments in call to PyErr_GivenExceptionMatches in pybind11::error_already_set.matches * Added tests on error_already_set::matches fix for exception base classes
This commit is contained in:
committed by
Wenzel Jakob
parent
a0b8f70df4
commit
97784dad3e
@@ -118,10 +118,38 @@ TEST_SUBMODULE(exceptions, m) {
|
||||
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"]; }
|
||||
try {
|
||||
// Assign to a py::object to force read access of nonexistent dict entry
|
||||
py::object o = foo["bar"];
|
||||
}
|
||||
catch (py::error_already_set& ex) {
|
||||
if (!ex.matches(PyExc_KeyError)) throw;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
m.def("exception_matches_base", []() {
|
||||
py::dict foo;
|
||||
try {
|
||||
// Assign to a py::object to force read access of nonexistent dict entry
|
||||
py::object o = foo["bar"];
|
||||
}
|
||||
catch (py::error_already_set &ex) {
|
||||
if (!ex.matches(PyExc_Exception)) throw;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
m.def("modulenotfound_exception_matches_base", []() {
|
||||
try {
|
||||
// On Python >= 3.6, this raises a ModuleNotFoundError, a subclass of ImportError
|
||||
py::module::import("nonexistent");
|
||||
}
|
||||
catch (py::error_already_set &ex) {
|
||||
if (!ex.matches(PyExc_ImportError)) throw;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
m.def("throw_already_set", [](bool err) {
|
||||
|
||||
Reference in New Issue
Block a user