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 documentation entry for warnings #5356

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions docs/advanced/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,30 @@ Alternately, to ignore the error, call `PyErr_Clear
Any Python error must be thrown or cleared, or Python/pybind11 will be left in
an invalid state.

Handling warnings from the Python C API
=====================================
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please add a couple more =, to line up with the text above?

(I overlooked this before.)


Where possible, use :ref:`pybind11 warnings <warnings>` instead of calling
Copy link
Collaborator

Choose a reason for hiding this comment

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

This :ref: does not work, just like the existing one above (line 307 here).

I don't know if you want to go in fixing the existing problem, up to you.

Here I'd simply write (i.e. replace this entire paragraph, lines 334-336, with):

Wrappers for handling Python warnings are implemented in ``pybind11/warnings.h``, which must be ``#include``ed explicitly (in other words, it is not transitively included with ``pybind11/pybind11.h``).

Copy link
Contributor Author

@jiwaszki jiwaszki Sep 30, 2024

Choose a reason for hiding this comment

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

I have copied your text over (with small change to fit docs requirements, "I don't know if you want to go in fixing the existing problem" -- I would not get into it right now...:)).

the Python C API directly. The motivation is similar to previously mentioned errors.
All warnings categories are required to be a subclass of `PyExc_Warning` from Python C API.

Warnings can be raised with `warn` function:

.. code-block:: cpp

py::warnings::warn("This is warning!", PyExc_Warning);

// When calling `stack_level` can be specified as well.
Copy link
Collaborator

Choose a reason for hiding this comment

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

   // Optionally, `stack_level` can be specified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it! It's way simpler... 😄

py::warnings::warn("Another one!", PyExc_DeprecationWarning, 3);

New warning types can be registered on the module level with `new_warning_type`:

.. code-block:: cpp

py::warnings::new_warning_type(m, "CustomWarning", PyExc_RuntimeWarning);

.. versionadded:: 2.14
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rwgk I cannot predict roadmap for versioning... is 2.14 okay?

Copy link
Collaborator

Choose a reason for hiding this comment

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

FYI, I'm switching employers (last day today, first day Monday). I'll try to get back here asap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rwgk Congrats and my best wishes for your next chapter!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Congrats and my best wishes for your next chapter!

Thanks!

.. versionadded:: 2.14

We only keep track of this in the Changelog. Please omit here.


Chaining exceptions ('raise from')
==================================

Expand Down
Loading