pitrou commented on code in PR #37797:
URL: https://github.com/apache/arrow/pull/37797#discussion_r1352027735
##########
python/pyarrow/table.pxi:
##########
@@ -2983,6 +2985,101 @@ cdef class RecordBatch(_Tabular):
<ArrowArray*> c_ptr, c_schema))
return pyarrow_wrap_batch(c_batch)
+ def __arrow_c_array__(self, requested_schema=None):
+ """
+ Get a pair of PyCapsules containing a C ArrowArray representation of
the object.
+
+ Parameters
+ ----------
+ requested_schema : PyCapsule | None
+ A PyCapsule containing a C ArrowSchema representation of a
requested
+ schema. PyArrow will attempt to cast the batch to this schema.
+ If None, the schema will be returned as-is, with a schema matching
the
+ one returned by :meth:`__arrow_c_schema__()`.
+
+ Returns
+ -------
+ Tuple[PyCapsule, PyCapsule]
+ A pair of PyCapsules containing a C ArrowSchema and ArrowArray,
+ respectively.
+ """
+ cdef:
+ ArrowArray* c_array = <ArrowArray*> malloc(sizeof(ArrowArray))
+ ArrowSchema* c_schema = <ArrowSchema*> malloc(sizeof(ArrowSchema))
Review Comment:
Here as well, we'll need to `free` in case an error occurs before creating
the capsules.
Another possible idiom would be to create the capsules upfront and do the
rest afterwards. Something like:
```cython
cdef object alloc_c_schema(ArrowSchema** c_schema):
c_schema[0] = <ArrowSchema*> malloc(sizeof(ArrowSchema))
# Ensure the capsule destructor doesn't call a random release pointer
c_schema[0].release = NULL
return PyCapsule_New(c_schema[0], 'arrow_schema',
&pycapsule_schema_deleter)
cdef object alloc_c_array(ArrowArray** c_array):
c_array[0] = <ArrowSchema*> malloc(sizeof(ArrowArray))
c_array[0].release = NULL
return PyCapsule_New(c_array[0], 'arrow_array', &pycapsule_array_deleter)
cdef class RecordBatch:
def __arrow_c_array__(self, requested_schema=None):
cdef:
ArrowArray* c_array
ArrowSchema* c_schema
schema_capsule = alloc_c_schema(&c_schema)
array_capsule = alloc_c_array(&c_array)
# Export logic below, possibly raising an exception
```
--
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]