diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index d63c2f184..63ed634f5 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -12,8 +12,8 @@ #include "detail/common.h" #include "buffer_info.h" +#include #include -#include #include #include #include @@ -367,13 +367,6 @@ std::string error_string(); std::string error_string(PyObject *, PyObject *, PyObject *); PYBIND11_NAMESPACE_END(detail) -inline const char *class_name(PyObject *py) { - if (Py_TYPE(py) == &PyType_Type) { - return reinterpret_cast(py)->tp_name; - } - return Py_TYPE(py)->tp_name; -} - #if defined(_MSC_VER) # pragma warning(push) # pragma warning(disable : 4275 4251) @@ -396,7 +389,9 @@ public: } error_already_set(const error_already_set &) = default; - error_already_set(error_already_set &&) = default; + error_already_set(error_already_set &&e) noexcept + : std::runtime_error(e), m_type{std::move(e.m_type)}, m_value{std::move(e.m_value)}, + m_trace{std::move(e.m_trace)}, m_lazy_what{std::move(e.m_lazy_what)} {}; inline ~error_already_set() override; @@ -415,7 +410,7 @@ public: if (m_type.ptr() == nullptr) { msg += "PYTHON_EXCEPTION_TYPE_IS_NULLPTR"; } else { - const char *tp_name = class_name(m_type.ptr()); + const char *tp_name = get_class_name(m_type.ptr()); if (tp_name == nullptr) { msg += "PYTHON_EXCEPTION_TP_NAME_IS_NULLPTR"; } else { @@ -436,7 +431,11 @@ public: + '"'; } } - std::cerr << msg << std::endl; + // Use C calls to reduce include bloat + fprintf(stderr, "%s\n", msg.c_str()); + fflush(stderr); + fprintf(stdout, "%s\n", msg.c_str()); + fflush(stdout); std::terminate(); } } @@ -490,8 +489,15 @@ public: const object &trace() const { return m_trace; } private: - mutable std::string m_lazy_what; object m_type, m_value, m_trace; + mutable std::string m_lazy_what; + + static const char *get_class_name(PyObject *py) { + if (Py_TYPE(py) == &PyType_Type) { + return reinterpret_cast(py)->tp_name; + } + return Py_TYPE(py)->tp_name; + } }; #if defined(_MSC_VER) # pragma warning(pop)