-
Notifications
You must be signed in to change notification settings - Fork 778
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
change PyModule::add_class to return an error if class creation fails #2947
Conversation
160b80f
to
1b84de3
Compare
On reflection I've renamed I've also pushed a follow-up commit which switches this to store |
1b84de3
to
2116d00
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks much better with the follow-up commit! Just one question where I am not sure about the performance characteristics.
2116d00
to
84ca932
Compare
Thanks! I've adjusted the comment around the code you've questioned, replied, and will proceed to merge. If we think there's follow-up work I'll open a new PR. |
bors r=adamreichold |
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>
Build failed: |
84ca932
to
c7cc48f
Compare
bors retry |
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>
Build failed: |
bors r+ |
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>
0a83c57
to
577570e
Compare
Canceled. |
bors retry |
Build succeeded: |
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 returnPyResult
instead, so that we can handle the error more appropriately further down the pipeline.For example, take the following snippet:
Currently, importing this module will fail with a panic:
After this PR, this import still fails, but with a slightly cleaner, more Pythonic error: