Uh oh!
There was an error while loading. Please reload this page.
Convert some std error types to PyErr - #22
Conversation
fafhrd91
left a comment
There was a problem hiding this comment.
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
| 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()) |
There was a problem hiding this comment.
This should be more precise, check utils.rs from async-tokio
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 |
fafhrd91
commented
Jun 7, 2017
Another option is to modify PyErr and just store errors unconverted. And convert them inside |
messense
commented
Jun 7, 2017
I known it's unsafe, but without it you would more likely to just I'd like to look into other options you mentioned to make it safer. |
messense
commented
Jun 7, 2017
I added a The ideal way would be fnmy_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;fnmy_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
commented
Jun 7, 2017
did you look into this feature? rust-lang/rust#42275 |
fafhrd91
commented
Jun 7, 2017
Lgtm. Could you also update async-tokio |
Address #21