Commit 16f199f8 by Yannick Jadoul Committed by GitHub

Change base parameter type in register_exception and exception constructor from…

Change base parameter type in register_exception and exception constructor from PyObject* to handle (#2467)

* Change base parameter type in register_exception and excepion constructor from PyObject* to handle

* Fix compilation error passing `handle` to `PyObject*`
parent e7bafc8e
...@@ -80,7 +80,7 @@ module and automatically converts any encountered exceptions of type ``CppExp`` ...@@ -80,7 +80,7 @@ module and automatically converts any encountered exceptions of type ``CppExp``
into Python exceptions of type ``PyExp``. into Python exceptions of type ``PyExp``.
It is possible to specify base class for the exception using the third It is possible to specify base class for the exception using the third
parameter, a pointer to `PyObject`: parameter, a `handle`:
.. code-block:: cpp .. code-block:: cpp
......
...@@ -1868,10 +1868,10 @@ template <typename type> ...@@ -1868,10 +1868,10 @@ template <typename type>
class exception : public object { class exception : public object {
public: public:
exception() = default; 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>() + std::string full_name = scope.attr("__name__").cast<std::string>() +
std::string(".") + name; std::string(".") + name;
m_ptr = PyErr_NewException(const_cast<char *>(full_name.c_str()), base, NULL); m_ptr = PyErr_NewException(const_cast<char *>(full_name.c_str()), base.ptr(), NULL);
if (hasattr(scope, name)) if (hasattr(scope, name))
pybind11_fail("Error during initialization: multiple incompatible " pybind11_fail("Error during initialization: multiple incompatible "
"definitions with name \"" + std::string(name) + "\""); "definitions with name \"" + std::string(name) + "\"");
...@@ -1901,7 +1901,7 @@ PYBIND11_NAMESPACE_END(detail) ...@@ -1901,7 +1901,7 @@ PYBIND11_NAMESPACE_END(detail)
template <typename CppException> template <typename CppException>
exception<CppException> &register_exception(handle scope, exception<CppException> &register_exception(handle scope,
const char *name, const char *name,
PyObject *base = PyExc_Exception) { handle base = PyExc_Exception) {
auto &ex = detail::get_exception_object<CppException>(); auto &ex = detail::get_exception_object<CppException>();
if (!ex) ex = exception<CppException>(scope, name, base); if (!ex) ex = exception<CppException>(scope, name, base);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment