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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to