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*`
This commit is contained in:
Yannick Jadoul 2020-09-15 16:24:39 +02:00 committed by GitHub
parent e7bafc8ec1
commit 16f199f8d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 4 deletions

View File

@ -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

View File

@ -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)
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);