-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2947: change PyModule::add_class to return an error if class creation fails r=adamreichold a=davidhewitt Related to #2942 At the moment there are panics deep in the `#[pyclass]` machinery when initialising the type fails. This PR adjusts a number of these functions to return `PyResult` instead, so that we can handle the error more appropriately further down the pipeline. For example, take the following snippet: ```rust #[pyclass(extends = PyBool)] struct ExtendsBool; #[pymodule] fn pyo3_scratch(_py: Python, m: &PyModule) -> PyResult<()> { m.add_class::<ExtendsBool>()?; Ok(()) } ``` Currently, importing this module will fail with a panic: ``` TypeError: type 'bool' is not an acceptable base type thread '<unnamed>' panicked at 'An error occurred while initializing class ExtendsBool', /Users/david/Dev/pyo3/src/pyclass.rs:412:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace Traceback (most recent call last): File "<string>", line 1, in <module> File "/Users/david/.virtualenvs/pyo3/lib/python3.10/site-packages/pyo3_scratch/__init__.py", line 1, in <module> from .pyo3_scratch import * pyo3_runtime.PanicException: An error occurred while initializing class ExtendsBool ``` After this PR, this import still fails, but with a slightly cleaner, more Pythonic error: ``` TypeError: type 'bool' is not an acceptable base type The above exception was the direct cause of the following exception: Traceback (most recent call last): File "<string>", line 1, in <module> File "/Users/david/.virtualenvs/pyo3/lib/python3.10/site-packages/pyo3_scratch/__init__.py", line 1, in <module> from .pyo3_scratch import * RuntimeError: An error occurred while initializing class ExtendsBool ``` Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Co-authored-by: Adam Reichold <adam.reichold@t-online.de>
- Loading branch information
Showing
10 changed files
with
209 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improve exception raised when creating `#[pyclass]` type object fails during module import. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.