paleolimbot commented on code in PR #451:
URL: https://github.com/apache/arrow-nanoarrow/pull/451#discussion_r1588225773


##########
python/src/nanoarrow/_lib.pyx:
##########
@@ -160,68 +160,93 @@ cdef void pycapsule_array_view_deleter(object 
array_capsule) noexcept:
     ArrowFree(array_view)
 
 
-cdef object alloc_c_array_view(ArrowArrayView** c_array_view) noexcept:
+cdef object alloc_c_array_view(ArrowArrayView** c_array_view):
     c_array_view[0] = <ArrowArrayView*> ArrowMalloc(sizeof(ArrowArrayView))
     ArrowArrayViewInitFromType(c_array_view[0], NANOARROW_TYPE_UNINITIALIZED)
     return PyCapsule_New(c_array_view[0], 'nanoarrow_array_view', 
&pycapsule_array_view_deleter)
 
 
-cdef void arrow_array_release(ArrowArray* array) noexcept with gil:
-    Py_DECREF(<object>array.private_data)
-    array.private_data = NULL
-    array.release = NULL
+# Provide a way to validate that we release all references we create
+cdef int64_t pyobject_buffer_count = 0
 
+def get_pyobject_buffer_count():
+    global pyobject_buffer_count
+    return pyobject_buffer_count
 
-cdef void c_array_shallow_copy(object base, const ArrowArray* c_array,
-                                 ArrowArray* c_array_out) noexcept:
-    # shallow copy
-    memcpy(c_array_out, c_array, sizeof(ArrowArray))
-    c_array_out.release = NULL
-    c_array_out.private_data = NULL
 
-    # track original base
-    c_array_out.private_data = <void*>base
+cdef void c_deallocate_pyobject_buffer(ArrowBufferAllocator* allocator, 
uint8_t* ptr, int64_t size) noexcept with gil:
+    Py_DECREF(<object>allocator.private_data)
+
+    global pyobject_buffer_count
+    pyobject_buffer_count -= 1
+
+
+cdef void c_pyobject_buffer(object base, const void* buf, int64_t size_bytes, 
ArrowBuffer* out):
+    out.data = <uint8_t*>buf
+    out.size_bytes = size_bytes
+    out.allocator = ArrowBufferDeallocator(
+        <ArrowBufferDeallocatorCallback>c_deallocate_pyobject_buffer,
+        <void*>base
+    )
     Py_INCREF(base)
-    c_array_out.release = arrow_array_release
 
+    global pyobject_buffer_count
+    pyobject_buffer_count += 1
 
-cdef object alloc_c_array_shallow_copy(object base, const ArrowArray* c_array):
-    """Make a shallow copy of an ArrowArray
 
-    To more safely implement export of an ArrowArray whose address may be
-    depended on by some other Python object, we implement a shallow copy
-    whose constructor calls Py_INCREF() on a Python object responsible
-    for the ArrowArray's lifecycle and whose deleter calls Py_DECREF() on
-    the same object.
-    """
-    cdef ArrowArray* c_array_out
-    array_capsule = alloc_c_array(&c_array_out)
-    c_array_shallow_copy(base, c_array, c_array_out)
-    return array_capsule
+cdef void c_array_shallow_copy(object base, const ArrowArray* src, ArrowArray* 
dst):
+    # Allocate an ArrowArray* that will definitely be cleaned up should an 
exception
+    # be raised in the process of shallow copying its contents
+    cdef ArrowArray* tmp
+    shelter = alloc_c_array(&tmp)
+    cdef int code
 
+    code = ArrowArrayInitFromType(tmp, NANOARROW_TYPE_UNINITIALIZED)
+    Error.raise_error_not_ok("ArrowArrayInitFromType()", code)
 
-cdef void c_device_array_shallow_copy(object base, const ArrowDeviceArray* 
c_array,
-                                      ArrowDeviceArray* c_array_out) noexcept:
-    # shallow copy
-    memcpy(c_array_out, c_array, sizeof(ArrowDeviceArray))
-    c_array_out.array.release = NULL
-    c_array_out.array.private_data = NULL
+    # Copy data for this array, adding a reference for each buffer
+    # This allows us to use nanoarrow's ArrowArray implementation without
+    # writing our own release callbacks/private_data.

Review Comment:
   I added a comment here to specifically refer to the `ArrowArray` release 
callback provided by `ArrowArrayInitFromType()` (which walks over a private 
array of `ArrowBuffer` and calls `ArrowBufferReset()`).



-- 
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