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

Cannot except PanicException in python #2880

Closed
fzyzcjy opened this issue Jan 14, 2023 · 7 comments
Closed

Cannot except PanicException in python #2880

fzyzcjy opened this issue Jan 14, 2023 · 7 comments
Labels

Comments

@fzyzcjy
Copy link

fzyzcjy commented Jan 14, 2023

Bug Description

Hi thanks for the helpful crate! I need to catch rust panics in python. Example code:

def test_catch_rust_panic():
    caught = False
    try:
        deliberate_panic()
    except Exception as e:
        assert 'deliberate panic' in str(e)
        caught = True
    assert caught
pub fn deliberate_panic() -> () { panic!("this is deliberate panic") }

However it does not work, error log:

    def deliberate_panic(self) -> None:
>       self.inner.deliberate_panic()
E       pyo3_runtime.PanicException: this is deliberate panic

../../rust_wrapper/ineq.py:75: PanicException

Yes I can change it to BaseException but that will capture a lot of other exceptions which I should not catch.

I have tried to import pyo3_runtime but seems that package does not exist at all.

Steps to Reproduce

see above

Backtrace

No response

Your operating system and version

macos

Your Python version (python --version)

3.9

Your Rust version (rustc --version)

1.66.1

Your PyO3 version

0.17.3

How did you install python? Did you use a virtualenv?

pip

Additional Info

No response

@fzyzcjy fzyzcjy added the bug label Jan 14, 2023
@adamreichold
Copy link
Member

Due to ABI issues/static linkage, every PyO3-based extension has its own PanicException. Meaning that to explicitly catch it, you need to explicitly make it part of your Python API, e.g. by calling

module.add("PanicException", <pyo3::panic::PanicException as pyo3::PyTypeInfo>::type_object(py))?;

on a suitable module: &pyo3::types::PyModule and import from there on the Python side.

@mejrs
Copy link
Member

mejrs commented Jan 14, 2023

Yes I can change it to BaseException but that will capture a lot of other exceptions which I should not catch.

This is also true for PanicException - you shouldn't catch it, you should fix the bugs in your code.

@adamreichold
Copy link
Member

latest

As an aside, please try to be specific here. At the moment, we all know what we are talking about. But the issue might be read by other people in the future where "latest" does not apply any more.

@fzyzcjy
Copy link
Author

fzyzcjy commented Jan 14, 2023

@adamreichold Thank you! So it is possible to catch all such PanicExceptions in different modules?

@mejrs This is also true for PanicException - you shouldn't catch it, you should fix the bugs in your code.

I see. Indeed I am using it for some machine learning data generation, and bugs are acceptable as long as it is not frequent - I just throw away that part of data and restart randomly.

latest

changed :)

@adamreichold
Copy link
Member

adamreichold commented Jan 15, 2023

@adamreichold Thank you! So it is possible to catch all such PanicExceptions in different modules?

I don't think so. As each Python extension built using PyO3 has a different PanicException (meaning different type objects), you need to import and catch all of them. (And I think you are out of luck for extensions which do not export their exception type at all.)

One workaround that might work is to catch BaseException and do something questionable like

except BaseException as e:
  if not "PanicException" in str(type(e)):
    raise

@fzyzcjy
Copy link
Author

fzyzcjy commented Jan 15, 2023

Thank you!

@davidhewitt
Copy link
Member

As this is now resolved and there's no intention to change PyO3's design here, I'm going to close this issue. Thanks both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants