diff --git a/include/pybind11/common.h b/include/pybind11/common.h index bbe92dfde..7299fbf81 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -189,6 +189,10 @@ extern "C" { } \ try { \ return pybind11_init(); \ + } catch (pybind11::error_already_set &e) { \ + e.clear(); \ + PyErr_SetString(PyExc_ImportError, e.what()); \ + return nullptr; \ } catch (const std::exception &e) { \ PyErr_SetString(PyExc_ImportError, e.what()); \ return nullptr; \ @@ -561,6 +565,9 @@ public: /// Give the error back to Python void restore() { PyErr_Restore(type, value, trace); type = value = trace = nullptr; } + /// Clear the held Python error state (the C++ `what()` message remains intact) + void clear() { restore(); PyErr_Clear(); } + private: PyObject *type, *value, *trace; }; diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 05d6b068b..006a4ff6c 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1884,8 +1884,7 @@ class gil_scoped_release { }; error_already_set::~error_already_set() { if (value) { gil_scoped_acquire gil; - PyErr_Restore(type, value, trace); - PyErr_Clear(); + clear(); } }