Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note about specifying custom base class for Exceptions. #2465

Merged
merged 3 commits into from
Sep 6, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/advanced/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ This call creates a Python exception class with the name ``PyExp`` in the given
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
YannickJadoul marked this conversation as resolved.
Show resolved Hide resolved
parameter, a pointer to `PyObject`:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is currently correct, but do we have any idea why this isn't a handle?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea, first time contributing to pybind.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Let's see if @bstaletic or anyone else knows! :-)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, no clue other than "the source says so".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's maybe change this in a different PR, then? (Actually, we might even change this to py::type, @henryiii?)


.. code-block:: cpp

py::register_exception<CppExp>(module, "PyExp", PyExc_RuntimeError);

Then `PyExp` can be caught both as `PyExp` and `RuntimeError`.
michalsustr marked this conversation as resolved.
Show resolved Hide resolved
Common base classes from the table above are `PyExc_Exception` (default),
`PyExc_IndexError`, `PyExc_KeyError`, `PyExc_MemoryError`, `PyExc_OverflowError`,
`PyExc_RuntimeError`, `PyExc_StopIteration`, `PyExc_ValueError`.
More definitions can be found in Python's `pyerrors.h` file, and they follow
the pattern `PyExc_XXX`.
michalsustr marked this conversation as resolved.
Show resolved Hide resolved

When more advanced exception translation is needed, the function
``py::register_exception_translator(translator)`` can be used to register
functions that can translate arbitrary exception types (and which may include
Expand Down