You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Tpt (via GitHub)" <gi...@apache.org> on 2026/04/03 15:44:05 UTC
[PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Tpt opened a new pull request, #9660:
URL: https://github.com/apache/arrow-rs/pull/9660
Remove validate_pycapsule: pointer_checked already raises an exception if the capsule type is wrong, and we wrap it into a nicer error
Follow up of #9639 #9594
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer commented on code in PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#discussion_r3428224325
##########
arrow-pyarrow/src/lib.rs:
##########
@@ -628,3 +544,78 @@ impl<T> From<T> for PyArrowType<T> {
Self(s)
}
}
+
+fn call_capsule_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<Bound<'py, PyCapsule>>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(
+ |e: CastError| {
+ wrapping_type_error(
+ object.py(),
+ e.into(),
+ format!("Expected {method_name} to return a capsule."),
+ )
+ },
+ )?))
+}
+
+fn call_capsule_pair_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(|e| {
+ wrapping_type_error(
+ object.py(),
+ e,
+ format!("Expected {method_name} to return a pair of capsules."),
+ )
+ })?))
+}
+
+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";
+}
Review 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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer commented on code in PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#discussion_r3428230822
##########
arrow-pyarrow/src/lib.rs:
##########
@@ -628,3 +544,78 @@ impl<T> From<T> for PyArrowType<T> {
Self(s)
}
}
+
+fn call_capsule_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<Bound<'py, PyCapsule>>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(
+ |e: CastError| {
+ wrapping_type_error(
+ object.py(),
+ e.into(),
+ format!("Expected {method_name} to return a capsule."),
+ )
+ },
+ )?))
+}
+
+fn call_capsule_pair_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(|e| {
+ wrapping_type_error(
+ object.py(),
+ e,
+ format!("Expected {method_name} to return a pair of capsules."),
+ )
+ })?))
+}
+
+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";
+}
Review Comment:
Other than that, it's a very nice cleanup and I may steal this idea over in `datafusion-python`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "Tpt (via GitHub)" <gi...@apache.org>.
Tpt commented on code in PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#discussion_r3429254975
##########
arrow-pyarrow/src/lib.rs:
##########
@@ -628,3 +544,78 @@ impl<T> From<T> for PyArrowType<T> {
Self(s)
}
}
+
+fn call_capsule_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<Bound<'py, PyCapsule>>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(
+ |e: CastError| {
+ wrapping_type_error(
+ object.py(),
+ e.into(),
+ format!("Expected {method_name} to return a capsule."),
+ )
+ },
+ )?))
+}
+
+fn call_capsule_pair_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(|e| {
+ wrapping_type_error(
+ object.py(),
+ e,
+ format!("Expected {method_name} to return a pair of capsules."),
+ )
+ })?))
+}
+
+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";
+}
Review Comment:
Trait removed: 2ab7223b416075696fb8290b4a7818c6c7bf6277
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#issuecomment-4771412425
Thank you @Tpt @timsaucer and @kylebarron
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "Tpt (via GitHub)" <gi...@apache.org>.
Tpt commented on PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#issuecomment-4726402667
@timsaucer I would love to get some review on this MR. It cleans up both code and error messages
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "Tpt (via GitHub)" <gi...@apache.org>.
Tpt commented on code in PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#discussion_r3428255563
##########
arrow-pyarrow/src/lib.rs:
##########
@@ -628,3 +544,78 @@ impl<T> From<T> for PyArrowType<T> {
Self(s)
}
}
+
+fn call_capsule_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<Bound<'py, PyCapsule>>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(
+ |e: CastError| {
+ wrapping_type_error(
+ object.py(),
+ e.into(),
+ format!("Expected {method_name} to return a capsule."),
+ )
+ },
+ )?))
+}
+
+fn call_capsule_pair_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(|e| {
+ wrapping_type_error(
+ object.py(),
+ e,
+ format!("Expected {method_name} to return a pair of capsules."),
+ )
+ })?))
+}
+
+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";
+}
Review Comment:
Nice! I have also ideas for follow ups (use string interning...). Very happy to help also on the `datafusion-python` side.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "Tpt (via GitHub)" <gi...@apache.org>.
Tpt commented on code in PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#discussion_r3429254975
##########
arrow-pyarrow/src/lib.rs:
##########
@@ -628,3 +544,78 @@ impl<T> From<T> for PyArrowType<T> {
Self(s)
}
}
+
+fn call_capsule_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<Bound<'py, PyCapsule>>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(
+ |e: CastError| {
+ wrapping_type_error(
+ object.py(),
+ e.into(),
+ format!("Expected {method_name} to return a capsule."),
+ )
+ },
+ )?))
+}
+
+fn call_capsule_pair_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(|e| {
+ wrapping_type_error(
+ object.py(),
+ e,
+ format!("Expected {method_name} to return a pair of capsules."),
+ )
+ })?))
+}
+
+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";
+}
Review Comment:
Trait removed: 2ab7223b416075696fb8290b4a7818c6c7bf6277 (fun cast, the number of LOC has not changed)
##########
arrow-pyarrow/src/lib.rs:
##########
@@ -628,3 +544,78 @@ impl<T> From<T> for PyArrowType<T> {
Self(s)
}
}
+
+fn call_capsule_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<Bound<'py, PyCapsule>>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(
+ |e: CastError| {
+ wrapping_type_error(
+ object.py(),
+ e.into(),
+ format!("Expected {method_name} to return a capsule."),
+ )
+ },
+ )?))
+}
+
+fn call_capsule_pair_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(|e| {
+ wrapping_type_error(
+ object.py(),
+ e,
+ format!("Expected {method_name} to return a pair of capsules."),
+ )
+ })?))
+}
+
+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";
+}
Review Comment:
Trait removed: 2ab7223b416075696fb8290b4a7818c6c7bf6277 (fun fact, the number of LOC has not changed)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "kylebarron (via GitHub)" <gi...@apache.org>.
kylebarron commented on code in PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#discussion_r3430447969
##########
arrow-pyarrow-testing/tests/pyarrow.rs:
##########
@@ -120,7 +119,7 @@ value = NotATuple()
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."
Review 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
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#discussion_r3429147938
##########
arrow-pyarrow/src/lib.rs:
##########
@@ -628,3 +544,78 @@ impl<T> From<T> for PyArrowType<T> {
Self(s)
}
}
+
+fn call_capsule_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<Bound<'py, PyCapsule>>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(
+ |e: CastError| {
+ wrapping_type_error(
+ object.py(),
+ e.into(),
+ format!("Expected {method_name} to return a capsule."),
+ )
+ },
+ )?))
+}
+
+fn call_capsule_pair_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(|e| {
+ wrapping_type_error(
+ object.py(),
+ e,
+ format!("Expected {method_name} to return a pair of capsules."),
+ )
+ })?))
+}
+
+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";
+}
Review Comment:
I agree with @timsaucer -- keeping the code simpler would be my preference but this looks ok too
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "Tpt (via GitHub)" <gi...@apache.org>.
Tpt commented on code in PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#discussion_r3430790371
##########
arrow-pyarrow-testing/tests/pyarrow.rs:
##########
@@ -120,7 +119,7 @@ value = NotATuple()
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."
Review Comment:
Indeed. I have reverted the error message: 1f72b242413d9ff3612c787805bc57b20f9acb02
##########
arrow-pyarrow-testing/tests/pyarrow.rs:
##########
@@ -120,7 +119,7 @@ value = NotATuple()
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."
Review Comment:
Indeed. I have reverted the error message change: 1f72b242413d9ff3612c787805bc57b20f9acb02
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "Tpt (via GitHub)" <gi...@apache.org>.
Tpt commented on code in PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#discussion_r3428127710
##########
arrow-pyarrow/src/lib.rs:
##########
@@ -628,3 +544,78 @@ impl<T> From<T> for PyArrowType<T> {
Self(s)
}
}
+
+fn call_capsule_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<Bound<'py, PyCapsule>>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(
+ |e: CastError| {
+ wrapping_type_error(
+ object.py(),
+ e.into(),
+ format!("Expected {method_name} to return a capsule."),
+ )
+ },
+ )?))
+}
+
+fn call_capsule_pair_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(|e| {
+ wrapping_type_error(
+ object.py(),
+ e,
+ format!("Expected {method_name} to return a pair of capsules."),
+ )
+ })?))
+}
+
+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";
+}
Review 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.
No other motivation outside of this small "fool-proofing". Glad to remove it if you prefer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "kylebarron (via GitHub)" <gi...@apache.org>.
kylebarron commented on PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#issuecomment-4733844131
Sorry I can't tell what the difference is with this PR. What's the user-facing difference between before and after?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "Tpt (via GitHub)" <gi...@apache.org>.
Tpt commented on PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#issuecomment-4261946022
@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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "Tpt (via GitHub)" <gi...@apache.org>.
Tpt commented on PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#issuecomment-4734262322
> 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. 56218cd86634582683091ffc41707de88eec630c 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:
> TypeError: 'int' object is not an instance of 'PyCapsule'
After:
> TypeError: Expected __arrow_c_schema__ to return a capsule.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "Tpt (via GitHub)" <gi...@apache.org>.
Tpt commented on code in PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#discussion_r3428127710
##########
arrow-pyarrow/src/lib.rs:
##########
@@ -628,3 +544,78 @@ impl<T> From<T> for PyArrowType<T> {
Self(s)
}
}
+
+fn call_capsule_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<Bound<'py, PyCapsule>>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(
+ |e: CastError| {
+ wrapping_type_error(
+ object.py(),
+ e.into(),
+ format!("Expected {method_name} to return a capsule."),
+ )
+ },
+ )?))
+}
+
+fn call_capsule_pair_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(|e| {
+ wrapping_type_error(
+ object.py(),
+ e,
+ format!("Expected {method_name} to return a pair of capsules."),
+ )
+ })?))
+}
+
+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";
+}
Review 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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] pyarrow: Wrap capsule extraction errors into an explicit error [arrow-rs]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer commented on code in PR #9660:
URL: https://github.com/apache/arrow-rs/pull/9660#discussion_r3428094614
##########
arrow-pyarrow/src/lib.rs:
##########
@@ -628,3 +544,78 @@ impl<T> From<T> for PyArrowType<T> {
Self(s)
}
}
+
+fn call_capsule_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<Bound<'py, PyCapsule>>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(
+ |e: CastError| {
+ wrapping_type_error(
+ object.py(),
+ e.into(),
+ format!("Expected {method_name} to return a capsule."),
+ )
+ },
+ )?))
+}
+
+fn call_capsule_pair_method_if_exists<'py>(
+ object: &Bound<'py, PyAny>,
+ method_name: &'static str,
+) -> PyResult<Option<(Bound<'py, PyCapsule>, Bound<'py, PyCapsule>)>> {
+ let Some(method) = object.getattr_opt(method_name)? else {
+ return Ok(None);
+ };
+ Ok(Some(method.call0()?.extract().map_err(|e| {
+ wrapping_type_error(
+ object.py(),
+ e,
+ format!("Expected {method_name} to return a pair of capsules."),
+ )
+ })?))
+}
+
+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";
+}
Review Comment:
This seems like overkill. Why not just keep these as constants like we had before and pass them as needed?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org