From 8f6ab3f16ff88506d61bfafe3159602193bf238f Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 6 May 2022 13:45:17 -0400 Subject: [PATCH 1/6] Try specifying noexcept again --- include/pybind11/pytypes.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index de9fa4627..afd2acbbd 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -395,8 +395,8 @@ public: } } - error_already_set(const error_already_set &) = default; - error_already_set(error_already_set &&) = default; + error_already_set(const error_already_set &) noexcept = default; + error_already_set(error_already_set &&) noexcept = default; inline ~error_already_set() override; From 4da996878ed097f94f177d96e0b1333be472d2f2 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 6 May 2022 14:28:35 -0400 Subject: [PATCH 2/6] Try explicit ctor --- include/pybind11/pytypes.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index afd2acbbd..7f66710c1 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -395,8 +395,13 @@ public: } } - error_already_set(const error_already_set &) noexcept = default; - error_already_set(error_already_set &&) noexcept = default; + error_already_set(const error_already_set &e) noexcept + : std::runtime_error(e), + m_lazy_what{e.m_lazy_what}, m_type{e.m_type}, m_value{e.m_value}, m_trace{e.m_trace} {}; + error_already_set(error_already_set &&e) noexcept + : std::runtime_error(e), m_lazy_what{std::move(e.m_lazy_what)}, + m_type{std::move(e.m_type)}, m_value{std::move(e.m_value)}, m_trace{ + std::move(e.m_trace)} {}; inline ~error_already_set() override; From f181dace8eb45ba0525b4bc76dc2dd0f782b2c5c Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 6 May 2022 14:39:59 -0400 Subject: [PATCH 3/6] default ctor is noexcept too --- include/pybind11/pytypes.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 7f66710c1..cef1e3171 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -388,7 +388,7 @@ class PYBIND11_EXPORT_EXCEPTION error_already_set : public std::runtime_error { public: /// Constructs a new exception from the current Python error indicator, if any. The current /// Python error indicator will be cleared. - error_already_set() : std::runtime_error("") { + error_already_set() noexcept : std::runtime_error("") { PyErr_Fetch(&m_type.ptr(), &m_value.ptr(), &m_trace.ptr()); if (m_type) { PyErr_NormalizeException(&m_type.ptr(), &m_value.ptr(), &m_trace.ptr()); From 6e781a0383f800d4a08c6d3013e2a99c52cb37a4 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sun, 8 May 2022 10:42:46 -0400 Subject: [PATCH 4/6] Apply reviewer suggestions, simplify code, and make helper method private --- include/pybind11/pytypes.h | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index cef1e3171..1e996c834 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -12,6 +12,7 @@ #include "detail/common.h" #include "buffer_info.h" +#include #include #include #include @@ -367,13 +368,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) @@ -388,20 +382,17 @@ class PYBIND11_EXPORT_EXCEPTION error_already_set : public std::runtime_error { public: /// Constructs a new exception from the current Python error indicator, if any. The current /// Python error indicator will be cleared. - error_already_set() noexcept : std::runtime_error("") { + error_already_set() : std::runtime_error("") { PyErr_Fetch(&m_type.ptr(), &m_value.ptr(), &m_trace.ptr()); if (m_type) { PyErr_NormalizeException(&m_type.ptr(), &m_value.ptr(), &m_trace.ptr()); } } - error_already_set(const error_already_set &e) noexcept - : std::runtime_error(e), - m_lazy_what{e.m_lazy_what}, m_type{e.m_type}, m_value{e.m_value}, m_trace{e.m_trace} {}; + error_already_set(const error_already_set &e) = default; error_already_set(error_already_set &&e) noexcept - : std::runtime_error(e), m_lazy_what{std::move(e.m_lazy_what)}, - m_type{std::move(e.m_type)}, m_value{std::move(e.m_value)}, m_trace{ - std::move(e.m_trace)} {}; + : 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; @@ -420,7 +411,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 { @@ -441,7 +432,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(); } } @@ -495,8 +490,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) From 1f8f0ab4c54149c32e85676d0e3292c63e7b3fb7 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sun, 8 May 2022 10:44:45 -0400 Subject: [PATCH 5/6] Remove unnecessary include --- include/pybind11/pytypes.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 1e996c834..01ab00558 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -14,7 +14,6 @@ #include #include -#include #include #include #include From 0aee425b3ba88e17c84ac0e5fb3759abbbbc4905 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sun, 8 May 2022 10:46:23 -0400 Subject: [PATCH 6/6] Clang-Tidy fix --- include/pybind11/pytypes.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 33333c7cb..63ed634f5 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -388,7 +388,7 @@ public: } } - error_already_set(const error_already_set &e) = default; + error_already_set(const 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)} {};