-
Notifications
You must be signed in to change notification settings - Fork 766
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
err: add PyErr::take
#1957
err: add PyErr::take
#1957
Conversation
@mejrs continuing from #1949 (comment) I think the most common usage of let result = unsafe { do_some_c_ffi(); }
if result == ERROR_VALUE {
return Err(PyErr::fetch(py));
} Signature of PyErr::fetch(py)?; would just continue the program, but if the C FFI return value indicated there is an error then there's something more deeply wrong. https://doc.rust-lang.org/std/net/struct.TcpStream.html#method.take_error - this is interesting. This makes me think the proposal to add https://docs.rs/async-std/1.10.0/async_std/io/struct.Error.html#method.last_os_error - this is also interesting and I was considering it too. If we wanted a What do you think is best? |
af37d7b
to
4e32e41
Compare
IMO, this is about "what makes it easy for users to do the right thing". I don't like having both of these: fetch(py) -> PyErr;
take(py) -> Option<PyErr> To me,
If some function returns an error sentinel value but no exception is set, that's considered a bug, right? I'd rather have only Footnotes
|
Agreed on all of those points, and that was my original motivation to suggest
Yes, and there was some debate about what to do in this case in this thread. In summary, we felt that the appropriate thing to do was to panic in debug mode and return
Agreed, and that's why I'd like us to expose the functionality of I was considering an alternative arrangement to fetch(py) -> Option<PyErr>;
last_error(py) -> PyErr // or api_call_failed(py) -> PyErr The advantage of exposing I wasn't entirely sure I liked either name |
I think I like that design, where it panics in debug mode and returns SystemError in release.
I agree. What about |
Hmm, What about |
Actually, I like that last one enough to open a PR - #1962 |
We could also mirror std's path api and have: fetch(py) -> PyErr;
try_fetch(py) -> Option<PyErr>; The main advantage of that is that it's not a breaking change. |
Avoiding the breaking change is definitely desirable if we're not totally sold on the alternative being better, for sure. I considered
|
Ok, to summarise where we're at: there are two main designs we have discussed:
fetch(py) -> Option<PyErr>;
fetch_last_error(py) -> PyErr;
fetch(py) -> PyErr;
take(py) -> Option<PyErr>;
// OR
try_fetch(py) -> PyResult<PyErr>; I'm personally leaning towards design 1. If we were to go for design 2, I think that having What's everyone else's preference? In the interest of making the release happen, I'd like to merge #1962 soon, perhaps tomorrow, if you folks are also happy with that. |
I don't love any of them, but I think I prefer: fetch(py) -> PyErr;
take(py) -> Option<PyErr>; We should add some documentation to both of these that say something along the lines of "if you receive some sort of error indicator and you call this function but it finds there's no exception set, that's a bug", and on |
@birkenfeld @messense - if you've got any preference towards design 1 or 2, would be really helpful to hear your inclination to help make a decision. (+ @indygreg too - I think I have seen you use |
I like adding |
Yeah, |
4e32e41
to
f801c19
Compare
Thanks all, sounds like this PR reverting the breakage to |
This is a resurrection of #1715.
The PRs I have seen in downstream projects to update PyO3 to 0.15 often have a number of changes around
PyErr::fetch
which add additional panics and would benefit from what we calledapi_call_failed
.This PR changes
PyErr::fetch
to just returnPyErr
again, and addsPyErr::take
which returnsOption<PyErr>
. I'm not convincedtake
is the perfect name for this function, but I can't think of anything better. (I briefly consideredfetch_if_set
, which is potentially clearer, although not exactly perfect either.)