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

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

Merged
merged 14 commits into from
Aug 8, 2023

Conversation

rwgk
Copy link
Collaborator

@rwgk rwgk commented Aug 2, 2023

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 Fix lifetime of JITException binding pytorch/pytorch#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.

Ralf W. Grosse-Kunstleve and others added 7 commits August 2, 2023 22:56
* Copy clang 17 compatibility fixes from PR pybind#4762 to a separate PR.

* Add gcc:13 C++20

* Add silkeh/clang:16-bullseye C++20
updates:
- [github.com/psf/black: 23.3.0 → 23.7.0](psf/black@23.3.0...23.7.0)
- [github.com/astral-sh/ruff-pre-commit: v0.0.276 → v0.0.281](astral-sh/ruff-pre-commit@v0.0.276...v0.0.281)
- [github.com/asottile/blacken-docs: 1.14.0 → 1.15.0](adamchainz/blacken-docs@1.14.0...1.15.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…tuptools (pybind#4774)

* docs: Remove upper bound on pybind11 in example pyproject.toml for setuptools

* Update docs/compiling.rst

---------

Co-authored-by: Henry Schreiner <[email protected]>
* Provide better type hints for a variety of generic types

* Makes better documentation
* tuple, dict, list, set, function

* Move to py::typing

* style: pre-commit fixes

* Update copyright line with correct year and actual author. The author information was copy-pasted from the git log output.

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…common.h).

Overload `py::set_error(py::handle, py::handle)`.
Change back to `static py::handle exc = ... .release();`
Deprecate `py::exception<>::operator()`
@rwgk rwgk changed the title WIP: static py::exception Add py::set_error(), use in updated py::exception<> documentation Aug 4, 2023
@rwgk rwgk mentioned this pull request Aug 4, 2023
Ralf W. Grosse-Kunstleve added 4 commits August 4, 2023 10:55
For ICC only, falling back to the recommended `py::set_error()` to keep the testing simple.

It is troublesome to add `--diag-disable=10441` specifically for test_exceptions.cpp, even that is non-ideal because it covers the entire file, not just the one line we need it for, and the value of exercising the trivial deprecated `operator()` on this one extra platform is practically zero.
…reated as errors, but falling back to using `py::set_error()` to not have to deal with that distraction.
@rwgk rwgk marked this pull request as ready for review August 4, 2023 19:56
@rwgk
Copy link
Collaborator Author

rwgk commented Aug 4, 2023

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

Copy link
Contributor

@albanD albanD left a comment

Choose a reason for hiding this comment

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

The new documentation and code looks good to me. It does make the doc surprisingly simpler to have set_error().
The rest of the update in the code looks good to me as well!

@rwgk
Copy link
Collaborator Author

rwgk commented Aug 8, 2023

Thanks for the reviews!

@rwgk rwgk merged commit 690a115 into pybind:master Aug 8, 2023
82 checks passed
@rwgk rwgk deleted the static_py_exception branch August 8, 2023 03:48
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Aug 8, 2023
@henryiii henryiii changed the title Add py::set_error(), use in updated py::exception<> documentation feat: add py::set_error(), use in updated py::exception<> documentation Nov 15, 2023
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants