This PR is much simpler than it might appear at first sight.
It adds two very simple py::set_error() functions in include/pybind11/pytypes.h
Sprawling but trivial changes: use the new functions everywhere to replace PyErr_SetString() and PyErr_SetObject() calls.
Update the py::exception<> documentation to not suggest code that may result in undefined behavior: the static py::exception<> destructor may run after the Python interpreter is finalized already.
Deprecate py::exception<>::operator(). It is evidently more likely to cause confusion (see https://github.com/pytorch/pytorch/pull/106401) than being helpful. Directing users to use py::set_error() universally is much simpler, there is nothing special to learn or remember.
Suggested changelog entry:
Two simple ``py::set_error()`` functions were added and the documentation was updated accordingly. In particular, ``py::exception<>::operator()`` was deprecated (use one of the new function instead). The documentation for ``py::exception<>`` was further updated to not suggest code that may result in undefined behavior.
Description
Related discussions:
This PR is much simpler than it might appear at first sight.
It adds two very simple
py::set_error()
functions in include/pybind11/pytypes.hSprawling but trivial changes: use the new functions everywhere to replace
PyErr_SetString()
andPyErr_SetObject()
calls.Update the
py::exception<>
documentation to not suggest code that may result in undefined behavior: thestatic py::exception<>
destructor may run after the Python interpreter is finalized already.Deprecate
py::exception<>::operator()
. It is evidently more likely to cause confusion (see https://github.com/pytorch/pytorch/pull/106401) than being helpful. Directing users to usepy::set_error()
universally is much simpler, there is nothing special to learn or remember.Suggested changelog entry: