From bab6f668e1e3a50557e924d61b9d1576e1a2d7a1 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 12 May 2022 14:26:01 -0700 Subject: [PATCH] For Python < 3.8: `PyErr_NormalizeException` before `PyErr_WriteUnraisable` --- include/pybind11/pytypes.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 45b771ebe..cb531b43e 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -472,7 +472,15 @@ public: /// this call, the current object no longer stores the error variables, and neither does /// Python. void discard_as_unraisable(object err_context) { +#if PY_VERSION_HEX >= 0x03080000 restore(); +#else + auto exc_type_loc = m_type; + auto exc_value_loc = m_value; + auto exc_trace_loc = m_trace; + PyErr_NormalizeException(&exc_type_loc.ptr(), &exc_value_loc.ptr(), &exc_trace_loc.ptr()); + PyErr_Restore(exc_type_loc.ptr(), exc_value_loc.ptr(), exc_trace_loc.ptr()); +#endif PyErr_WriteUnraisable(err_context.ptr()); } /// An alternate version of `discard_as_unraisable()`, where a string provides information on