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

Convert some std error types to PyErr #22

Merged
merged 2 commits into from
Jun 8, 2017
Merged

Convert some std error types to PyErr #22

merged 2 commits into from
Jun 8, 2017

Conversation

messense
Copy link
Member

@messense messense commented Jun 7, 2017

Address #21

@messense messense requested a review from fafhrd91 June 7, 2017 08:15
Copy link
Contributor

@fafhrd91 fafhrd91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is highly unsafe, if you call this conversions outside of GIL, python would crash. In general case ToPyObject trait does py memory allocation, which is require Gil

src/err.rs Outdated
impl std::convert::From<std::io::Error> for PyErr {
fn from(err: std::io::Error) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::IOError, _>(py, err.description())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be more precise, check utils.rs from async-tokio

@fafhrd91
Copy link
Contributor

fafhrd91 commented Jun 7, 2017

Maybe we can change PyResult type and include unconverted errors. Then we can use custom form of From trait in callback handler when we have python object available. Also we can use unstable try_traiy for implementing ?

@fafhrd91
Copy link
Contributor

fafhrd91 commented Jun 7, 2017

Another option is to modify PyErr and just store errors unconverted. And convert them inside PyErr::restore()

@messense
Copy link
Member Author

messense commented Jun 7, 2017

I known it's unsafe, but without it you would more likely to just unwrap() to workaround it which it not good either.

I'd like to look into other options you mentioned to make it safer.

@messense
Copy link
Member Author

messense commented Jun 7, 2017

I added a ToPyErr trait just like that in async-tokio.

The ideal way would be

fn my_py_fn(py: Python, path: &str) -> PyResult<String> {
  let f = File::open(path)?;
  ...
}

But that's a little complicated to implement now. With this PR we can do:

use pyo3::ToPyErr;

fn my_py_fn(py: Python, path: &str) -> PyResult<String> {
  let f = File::open(path).map_err(|e| e.to_pyerr(py))?;
  ...
}

which should be good enough for most cases.

@fafhrd91
Copy link
Contributor

fafhrd91 commented Jun 7, 2017

did you look into this feature? rust-lang/rust#42275

@fafhrd91
Copy link
Contributor

fafhrd91 commented Jun 7, 2017

Lgtm. Could you also update async-tokio

@messense messense merged commit e12e98a into PyO3:master Jun 8, 2017
@messense messense deleted the feature/std-error-to-pyerr branch June 8, 2017 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants