paleolimbot commented on code in PR #340:
URL: https://github.com/apache/arrow-nanoarrow/pull/340#discussion_r1447948643
##########
python/src/nanoarrow/_lib.pyx:
##########
@@ -24,45 +24,50 @@ This Cython extension provides low-level Python wrappers
around the
Arrow C Data and Arrow C Stream interface structs. In general, there
is one wrapper per C struct and pointer validity is managed by keeping
strong references to Python objects. These wrappers are intended to
-be literal and stay close to the structure definitions.
+be literal and stay close to the structure definitions: higher level
+interfaces can and should be built in Python where it is faster to
+iterate and where it is easier to create a better user experience
+by default (i.e., classes, methods, and functions implemented in Python
+generally have better autocomplete + documentation available to IDEs).
"""
from libc.stdint cimport uintptr_t, int64_t
-from libc.stdlib cimport malloc, free
from libc.string cimport memcpy
-from cpython.mem cimport PyMem_Malloc, PyMem_Free
+from libc.stdio cimport snprintf
from cpython.bytes cimport PyBytes_FromStringAndSize
-from cpython.pycapsule cimport PyCapsule_New, PyCapsule_GetPointer,
PyCapsule_CheckExact
+from cpython.pycapsule cimport PyCapsule_New, PyCapsule_GetPointer
from cpython cimport Py_buffer
-from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF
+from cpython.ref cimport Py_INCREF, Py_DECREF
from nanoarrow_c cimport *
from nanoarrow_device_c cimport *
-from nanoarrow._lib_utils import array_repr, device_array_repr, schema_repr,
device_repr
+from struct import unpack_from, iter_unpack
+from nanoarrow import _lib_utils
def c_version():
"""Return the nanoarrow C library version string
"""
return ArrowNanoarrowVersion().decode("UTF-8")
+# PyCapsule utilities
#
-# PyCapsule export utilities
-#
-
-
+# PyCapsules are used (1) to safely manage memory associated with C structures
+# by initializing them and ensuring the appropriate cleanup is invoked when
+# the object is deleted; and (2) as an export mechanism conforming to the
+# Arrow PyCapsule interface for the objects where this is defined.
cdef void pycapsule_schema_deleter(object schema_capsule) noexcept:
cdef ArrowSchema* schema = <ArrowSchema*>PyCapsule_GetPointer(
schema_capsule, 'arrow_schema'
)
if schema.release != NULL:
ArrowSchemaRelease(schema)
- free(schema)
+ ArrowFree(schema)
Review Comment:
I just did it for consistency with what I do in R, and because in theory we
could someday add some debug check or bookkeeping to debug
ArrowMalloc/ArrowFree (whereas we can't for malloc/free other than valgrind).
I'm not sure I will ever get to that, though 🙂
--
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]