-
Notifications
You must be signed in to change notification settings - Fork 763
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
Implement std::error::Error for PyErr #1115
Conversation
Thank you for this hard work and refactoring! It's well designed overall, but I want to list up some design questions before reviewing in detail:
|
Thanks, good points:
I'll push a commit tweaking as suggested tonight. |
(still planning to push fix later, sorry didn't get round to it yesterday.) |
412209c
to
80ea2e4
Compare
This is ready for a first round of review. Once we've made any design changes necessary, I also need to write more tests and the migration guide entry before we can merge this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While overhauling PyErr
, can we fix the pretty confusing and duplicate APIs around creating new errors?
I mean mostly PyConcreteError::py_err
and PyConcreteError::into
:
-
for me, the first doesn't really make the association with
PyErr
. I'd much rather this be callednew
: AFAICT, that method is not defined currently. -
the second reads really strange, since I'm hardwired to think of
into()
as a self-consuming method.
The two are also named completely differently, although the do very similar things (AFAICS, into()
is just Err(py_err())
). IMO into()
can be removed, I can't think of any other library that provides this shortcut.
I also think the PyErr: Into<PyResult>
impl is unidiomatic and should be removed.
👍 I'm completely happy to rework I'm definitely in favour of removing Regarding |
Yes, I was thinking about that too. |
Since #1024 it's possible to have a reference to all the exception objects in just the same way as for the rest of the native types (i.e. they're no longer void structs). |
The last piece of the puzzle, which I haven't finished yet but plan to do soon, is have a |
Oops, I missed that. Yes, then it makes sense not to call that |
|
BTW, could |
Sadly it's not possible to combine turbofish with
|
52779f0
to
3d33360
Compare
One last point of discussion: At the moment there's no implementation for At the moment we only offer this implementation for I think it should be possible to implement for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
But what is the |
It would be the normalized python exceptions's cause, as seen at https://docs.python.org/3/c-api/exceptions.html#c.PyException_GetCause |
Thanks for the comments, updated as suggested. I'll start writing docs and tests later 👍 |
96905d9
to
79dd892
Compare
Rebased, added some further test coverage, and also simplified I was able to take out the |
#[doc(hidden)] | ||
pub fn ensure_gil() -> EnsureGIL { | ||
/// Ensure the GIL is held, used in the implementation of Python::with_gil | ||
pub(crate) fn ensure_gil() -> EnsureGIL { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rust-numpy
will need to migrate to Python::with_gil
83e1a23
to
c4b4d4e
Compare
Right, I've just pushed a load of documentation for this. I now think this is ready to merge, so would appreciate some final reviews from anyone who's got a few minutes! |
7b4e0c8
to
d41c6d9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but it would be better if we have another review for the document.
@birkenfeld
Could you take a glance on it?
d41c6d9
to
fd65107
Compare
Thanks for the great feedback on the docs; I've pushed some quick changes based on your review. |
I'd like to merge this tonight; maybe @sebpuetz is interested in reviewing? |
Sure, I should find some time within the next hour |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't have time to do a very thorough review but this looks pretty good, just a handful of questions from my side.
fd65107
to
b9e95dc
Compare
Thanks for the review! In the end I didn't have time to merge yesterday evening. I've just pushed tweaks to address your comments and will merge shortly... |
In PyO3/pyo3#1115 the internal_utils module was removed from pyo3. There is a note in the review that rust-numpy will need to migrate to using with_gil() instead. This commit makes this change to enable running with pyo3 0.12.0.
This is a rework of
PyErr
to implementstd::error::Error
(andstd::fmt:Display
).The rework was necessary because functions like
PyErr::normalize()
taking&mut self
andPyErr::instance()
takingself
were incompatible withstd::error::Error
.A couple of notes about the implementation:
Display
forPyError
automatically usesPython::with_gil
internally. I think as this is the error path, and also it's rare that the GL will not already be acquired, this overhead is fine.PyErr
now contains anUnsafeCell
. The safety is assured because, once normalized, theUnsafeCell
value never changes. Almost every nontrivial access to thePyErr
immediately normalizes it.Closes #682
Closes #1034
TODO:
Lots of documentation / guide updates!Add more test coverage.RenamePyConcreteException::py_err
(... toPyConcreteException::new_err
?)std::error::Error::cause()
?