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

Expose PyErr_WriteUnraisable #2884

Closed
mitsuhiko opened this issue Jan 17, 2023 · 3 comments · Fixed by #2889
Closed

Expose PyErr_WriteUnraisable #2884

mitsuhiko opened this issue Jan 17, 2023 · 3 comments · Fixed by #2889

Comments

@mitsuhiko
Copy link
Contributor

mitsuhiko commented Jan 17, 2023

Since you sometimes might have some situations where Rust code that is infallible really likes to call into a Python callback, you end up with a horrible situation where all you can do is log the error.

However with Python 3.8 and later you can report such errors so they can be trapped. It would be nice to have a way to do this without unsafe code:

pub fn report_unraisable(py: Python<'_>, err: PyErr) {
    err.restore(py);
    unsafe {
        PyErr_WriteUnraisable(std::ptr::null_mut());
    }
}

I propose to add a write_unraisable() method directly onto PyErr for this purpose.

@davidhewitt
Copy link
Member

I fully support this! I've nearly added this in the past for various reasons (as a pub(crate) fn) while shuffling pieces of PyO3 around. The fix in #2886 actually ended up using PyErr_WriteUnraisable also.

Where do you see the limitation of 3.8 and later? My impression is that this is also available on 3.7 (i.e. oldest version of Python which we currently support).

@davidhewitt
Copy link
Member

Please feel free to send as PR if you wish, otherwise I'll get around to this within a couple weeks (before the next release at a minimum).

@mitsuhiko
Copy link
Contributor Author

Maybe it works on 3.7, I'm not sure. I took that information from sys.unraisablehook and I thought that not just the hook was new.

sys.unraisablehook(unraisable, /)
Handle an unraisable exception. […]

New in version 3.8.

I can try to make a PR when I find some time for it. I'm assuming it generally is probably relatively non controversial, particularly if it turns out it works with 3.7.

bors bot added a commit that referenced this issue Jan 27, 2023
2873: A new example that shows how to integrate Python plugins that use pyclasses into a Rust app r=davidhewitt a=alexpyattaev

Example showing integration of a Python plugin into a Rust app while having option to test pyclass based API without the main app.  This also illustrates some aspects related to import of Python modules into a Rust app while also having an API module available for the Python code to be able to produce Rust objects. 

CI seems to fail on my local machine for reasons unrelated to the example just added:
```
error: unused macro definition: `check_struct`
  --> pyo3-ffi-check/src/main.rs:13:18
   |
13 |     macro_rules! check_struct {
   |                  ^^^^^^^^^^^^
   |
```

2889: Added support for PyErr_WriteUnraisable r=davidhewitt a=mitsuhiko

Fixes #2884

2923: hygiene: fix `#[pymethods(crate = "...")]` r=davidhewitt a=davidhewitt

Got to the bottom of the hygiene issue in test of #2914 

Turns out that `#[pymethods] #[pyo3(crate = "...")]` works, but `#[pymethods(crate = "...")]` was ignoring the argument.

Added a tweak to fix this and a snippet in the hygiene test (which fails on `main`). 

2924: remove unneeded into_iter calls r=davidhewitt a=davidhewitt

Clippy complaining about these to me this morning locally.

Co-authored-by: Alex Pyattaev <alex.pyattaev@gmail.com>
Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
Co-authored-by: Armin Ronacher <armin.ronacher@active-4.com>
bors bot added a commit that referenced this issue Jan 27, 2023
2873: A new example that shows how to integrate Python plugins that use pyclasses into a Rust app r=davidhewitt a=alexpyattaev

Example showing integration of a Python plugin into a Rust app while having option to test pyclass based API without the main app.  This also illustrates some aspects related to import of Python modules into a Rust app while also having an API module available for the Python code to be able to produce Rust objects. 

CI seems to fail on my local machine for reasons unrelated to the example just added:
```
error: unused macro definition: `check_struct`
  --> pyo3-ffi-check/src/main.rs:13:18
   |
13 |     macro_rules! check_struct {
   |                  ^^^^^^^^^^^^
   |
```

2889: Added support for PyErr_WriteUnraisable r=davidhewitt a=mitsuhiko

Fixes #2884

Co-authored-by: Alex Pyattaev <alex.pyattaev@gmail.com>
Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
Co-authored-by: Armin Ronacher <armin.ronacher@active-4.com>
bors bot added a commit that referenced this issue Jan 27, 2023
2889: Added support for PyErr_WriteUnraisable r=davidhewitt a=mitsuhiko

Fixes #2884

Co-authored-by: Armin Ronacher <armin.ronacher@active-4.com>
@bors bors bot closed this as completed in cb38ff0 Jan 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants