Skip to content

Commit e2f86af

Browse files
jiwaszkirwgk
andauthored
docs: add documentation entry for warnings (#5356)
* [docs] Add entry for warnings * Fix code formatting * Update docs * Fix issue with docs * [skip ci] Minor edits. --------- Co-authored-by: Ralf W. Grosse-Kunstleve <[email protected]>
1 parent f3bb007 commit e2f86af

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/advanced/exceptions.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,28 @@ Alternately, to ignore the error, call `PyErr_Clear
328328
Any Python error must be thrown or cleared, or Python/pybind11 will be left in
329329
an invalid state.
330330

331+
Handling warnings from the Python C API
332+
=======================================
333+
334+
Wrappers for handling Python warnings are provided in ``pybind11/warnings.h``.
335+
This header must be included explicitly; it is not transitively included via
336+
``pybind11/pybind11.h``.
337+
338+
Warnings can be raised with the ``warn`` function:
339+
340+
.. code-block:: cpp
341+
342+
py::warnings::warn("This is a warning!", PyExc_Warning);
343+
344+
// Optionally, a `stack_level` can be specified.
345+
py::warnings::warn("Another one!", PyExc_DeprecationWarning, 3);
346+
347+
New warning types can be registered at the module level using ``new_warning_type``:
348+
349+
.. code-block:: cpp
350+
351+
py::warnings::new_warning_type(m, "CustomWarning", PyExc_RuntimeWarning);
352+
331353
Chaining exceptions ('raise from')
332354
==================================
333355

0 commit comments

Comments
 (0)