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

Question: How to convert Python Exception (PyErr) into string for logging #1034

Closed
rohitjoshi opened this issue Jul 11, 2020 · 3 comments · Fixed by #1115
Closed

Question: How to convert Python Exception (PyErr) into string for logging #1034

rohitjoshi opened this issue Jul 11, 2020 · 3 comments · Fixed by #1115
Milestone

Comments

@rohitjoshi
Copy link

I have a python script which raises exception. How do I print this in the rust log?

@davidhewitt
Copy link
Member

I'm afraid that there's no good API to format the exception as a Rust string at the moment.

In PyO3 0.12 I hope that you'll just be able to write something like:

log::error!("A Python exception occurred: {}", e.display())

or something very similar to this.

@rohitjoshi
Copy link
Author

Thanks @davidhewitt appreciate your quick response and looking forward to display() function.

In meantime, any other work around? I am even open to using eval() to retrieve from python module

@davidhewitt
Copy link
Member

I think the best you might be able to do is something like

e.restore(py);
let s: String = py.eval(r#"
import traceback
traceback.format_exception()
"#)?.extract(py)?;
let _ = PyErr::fetch();
log::error!("{}", s);

I've typed this out on my phone, so might have errors.

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