Skip to content

Extracting and matching Python Error in Rust #3759

Discussion options

You must be logged in to vote

I'm aware of the is_instance_of method, but is there a way without passing the Py token?

You definitely need a GIL token, but it should be easy to come by. Either you need to wrap your call to is_instance_of using with_gil, e.g.

fn is_key_error(err: &PyErr) -> bool {
    Python::with_gil(|py| {
        err.is_instance_of::<PyKeyError>(py)
    })
}

or better yet, you just add py: Python as an argument to your #[pyfunction] or #[pymethod] and PyO3 will provide you with a token. (The GIL is already held when PyO3 calls your function or method, hence we can just pass on the token if you request it.)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@MohamedAbdeen21
Comment options

Answer selected by MohamedAbdeen21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants