diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 8ff1871e7..d6af10ed8 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -415,8 +415,8 @@ PYBIND11_NOINLINE inline std::string error_string(PyObject *type, PyObject *valu if (value) result += str(value).cast(); -#if !defined(PYPY_VERSION) if (trace) { +#if !defined(PYPY_VERSION) PyTracebackObject *tb = (PyTracebackObject *) trace; // Get the deepest trace possible. @@ -433,8 +433,8 @@ PYBIND11_NOINLINE inline std::string error_string(PyObject *type, PyObject *valu handle(frame->f_code->co_name).cast() + "\n"; frame = frame->f_back; } - } #endif + } return result; } diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 2945d9133..df0cc70df 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -335,8 +335,10 @@ public: virtual const char* what() const noexcept override { if (m_lazy_what.empty()) { - try { + if (m_type) PyErr_NormalizeException(&m_type.ptr(), &m_value.ptr(), &m_trace.ptr()); + + try { m_lazy_what = detail::error_string(m_type.ptr(), m_value.ptr(), m_trace.ptr()); } catch (...) { return "Unknown internal error occurred";