pyarrow: Wrap capsule extraction errors into an explicit error - #9660
Conversation
aa910ff to
61391e3
Compare
|
@alamb Thank you for the rebase. This MR is blocked by a segfault. I have not understood why yet and put this MR on hold to go back to it with a fresh mind. I will try to debug that in the next few days. Sorry for having it been stalled for so long. Update: got it working, there was a use-after-free error. |
f0b61da to
7ecba54
Compare
b1880b6 to
240fd32
Compare
Remove validate_pycapsule: pointer_checked already raises an exception if the capsule type is wrong, and we wrap it into a nicer error
|
@timsaucer I would love to get some review on this MR. It cleans up both code and error messages |
timsaucer
left a comment
There was a problem hiding this comment.
It's a nice cleanup. Adding a trait that just contains a constant seems like it adds complexity with minimal value. Otherwise I'm good with the changes.
| trait PyCapsuleType { | ||
| const NAME: &CStr; | ||
| } | ||
|
|
||
| impl PyCapsuleType for FFI_ArrowSchema { | ||
| const NAME: &CStr = c"arrow_schema"; | ||
| } | ||
|
|
||
| impl PyCapsuleType for FFI_ArrowArray { | ||
| const NAME: &CStr = c"arrow_array"; | ||
| } | ||
|
|
||
| impl PyCapsuleType for FFI_ArrowArrayStream { | ||
| const NAME: &CStr = c"arrow_array_stream"; | ||
| } |
There was a problem hiding this comment.
This seems like overkill. Why not just keep these as constants like we had before and pass them as needed?
There was a problem hiding this comment.
My motivation for the trait is to have a single place to associate a type with its capsule name (the impl block) and to avoid the possible mistake of using a wrong capsule name at an extract_capsule call site. This also allows to keep the capsule name concern outside of the main code logic.
No other motivation outside of this small "fool-proofing". Glad to remove it if you prefer.
There was a problem hiding this comment.
I'm not a committer on this repo, so I'd leave it to them for final say. My recommendation would be to keep it simpler.
There was a problem hiding this comment.
Other than that, it's a very nice cleanup and I may steal this idea over in datafusion-python.
There was a problem hiding this comment.
Nice! I have also ideas for follow ups (use string interning...). Very happy to help also on the datafusion-python side.
There was a problem hiding this comment.
I agree with @timsaucer -- keeping the code simpler would be my preference but this looks ok too
There was a problem hiding this comment.
Trait removed: 2ab7223 (fun fact, the number of LOC has not changed)
alamb
left a comment
There was a problem hiding this comment.
Looks like a nice improvement to me -- thanks @Tpt
I defer to @timsaucer (or @kylebarron ) for the specifics
| trait PyCapsuleType { | ||
| const NAME: &CStr; | ||
| } | ||
|
|
||
| impl PyCapsuleType for FFI_ArrowSchema { | ||
| const NAME: &CStr = c"arrow_schema"; | ||
| } | ||
|
|
||
| impl PyCapsuleType for FFI_ArrowArray { | ||
| const NAME: &CStr = c"arrow_array"; | ||
| } | ||
|
|
||
| impl PyCapsuleType for FFI_ArrowArrayStream { | ||
| const NAME: &CStr = c"arrow_array_stream"; | ||
| } |
There was a problem hiding this comment.
I agree with @timsaucer -- keeping the code simpler would be my preference but this looks ok too
| assert!(err.is_instance_of::<PyTypeError>(py)); | ||
| assert_eq!( | ||
| err.to_string(), | ||
| "TypeError: Expected __arrow_c_array__ to return a tuple of (schema, array) capsules." |
There was a problem hiding this comment.
Not sure if it's necessary here because these are integration tests, not user-facing, but I think the old language was more explicit
There was a problem hiding this comment.
Indeed. I have reverted the error message change: 1f72b24
|
Sorry I can't tell what the difference is with this PR. What's the user-facing difference between before and after? |
Sorry, I should have made it more explicit. 56218cd add a test to highlight the change. Before the change, when calling a method that must return a capsule but does not actually return one:
After:
|
|
Thank you @Tpt @timsaucer and @kylebarron |
Remove validate_pycapsule: pointer_checked already raises an exception if the capsule type is wrong, and we wrap it into a nicer error
Also relies on a trait to associate types with their capsules names
Because we wrap the exception (equivalent of
raise X from Y), the original exception is still thereFollow up of #9639 #9594