From 9b6e84e37413bcffa27bede9f12d76877f1ee6e3 Mon Sep 17 00:00:00 2001 From: Yannick Jadoul Date: Sun, 6 Sep 2020 14:31:01 +0200 Subject: [PATCH 1/2] Change base parameter type in register_exception and excepion constructor from PyObject* to handle --- docs/advanced/exceptions.rst | 2 +- include/pybind11/pybind11.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/advanced/exceptions.rst b/docs/advanced/exceptions.rst index 874af4cc4c..a96f8e8f4d 100644 --- a/docs/advanced/exceptions.rst +++ b/docs/advanced/exceptions.rst @@ -80,7 +80,7 @@ module and automatically converts any encountered exceptions of type ``CppExp`` into Python exceptions of type ``PyExp``. It is possible to specify base class for the exception using the third -parameter, a pointer to `PyObject`: +parameter, a `handle`: .. code-block:: cpp diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 602d5790a2..821d5d4fbb 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1868,7 +1868,7 @@ template class exception : public object { public: exception() = default; - exception(handle scope, const char *name, PyObject *base = PyExc_Exception) { + exception(handle scope, const char *name, handle base = PyExc_Exception) { std::string full_name = scope.attr("__name__").cast() + std::string(".") + name; m_ptr = PyErr_NewException(const_cast(full_name.c_str()), base, NULL); @@ -1901,7 +1901,7 @@ PYBIND11_NAMESPACE_END(detail) template exception ®ister_exception(handle scope, const char *name, - PyObject *base = PyExc_Exception) { + handle base = PyExc_Exception) { auto &ex = detail::get_exception_object(); if (!ex) ex = exception(scope, name, base); From ae6f96426137cff99ba36cfb9ae8a747bffb7af8 Mon Sep 17 00:00:00 2001 From: Yannick Jadoul Date: Sun, 6 Sep 2020 14:57:38 +0200 Subject: [PATCH 2/2] Fix compilation error passing `handle` to `PyObject*` --- include/pybind11/pybind11.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 821d5d4fbb..150f4dee2c 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1871,7 +1871,7 @@ class exception : public object { exception(handle scope, const char *name, handle base = PyExc_Exception) { std::string full_name = scope.attr("__name__").cast() + std::string(".") + name; - m_ptr = PyErr_NewException(const_cast(full_name.c_str()), base, NULL); + m_ptr = PyErr_NewException(const_cast(full_name.c_str()), base.ptr(), NULL); if (hasattr(scope, name)) pybind11_fail("Error during initialization: multiple incompatible " "definitions with name \"" + std::string(name) + "\"");