pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.58k stars 2.09k forks source link

feat: add `py::set_error()`, use in updated `py::exception<>` documentation #4772

Closed rwgk closed 1 year ago

rwgk commented 1 year ago

Description

Related discussions:

This PR is much simpler than it might appear at first sight.

  1. It adds two very simple py::set_error() functions in include/pybind11/pytypes.h

  2. Sprawling but trivial changes: use the new functions everywhere to replace PyErr_SetString() and PyErr_SetObject() calls.

  3. 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.

  4. 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.
rwgk commented 1 year ago

@albanD Could you please review and let me know any suggestions?

rwgk commented 1 year ago

Thanks for the reviews!