paleolimbot commented on code in PR #378:
URL: https://github.com/apache/arrow-nanoarrow/pull/378#discussion_r1484929392
##########
python/src/nanoarrow/c_lib.py:
##########
@@ -125,10 +138,205 @@ def c_array(obj=None, requested_schema=None) -> CArray:
out = CArray.allocate(CSchema.allocate())
obj._export_to_c(out._addr(), out.schema._addr())
return out
- else:
+
+ # Try buffer protocol (e.g., numpy arrays)
+ try:
+ return c_array_from_pybuffer(obj)
+ except Exception as e:
raise TypeError(
f"Can't convert object of type {type(obj).__name__} to
nanoarrow.c_array"
+ ) from e
+
+
+def c_array_from_pybuffer(obj) -> CArray:
+ """Create an ArrowArray wrapper from the Python buffer protocol
+
+ Invokes the Python buffer protocol to wrap the buffer represented by obj
+ if possible.
+
+ Examples
+ --------
+
+ >>> import nanoarrow as na
+ >>> from nanoarrow.c_lib import c_array_from_pybuffer
+ >>> na.c_array_view(c_array_from_pybuffer(b"1234"))
+ <nanoarrow.c_lib.CArrayView>
+ - storage_type: 'uint8'
+ - length: 4
+ - offset: 0
+ - null_count: 0
+ - buffers[2]:
+ - validity <bool[0 b] >
+ - data <uint8[4 b] 49 50 51 52>
+ - dictionary: NULL
+ - children[0]:
+ """
+
+ buffer = CBuffer().set_pybuffer(obj)
+ view = buffer.data
Review Comment:
Ok! It was a bit of a goose chase, but implementing the buffer protocol on
the `CBuffer` (by caching a `CBufferView` on it) removed the need for `.data`
alltogether and made the code much better! In theory also safer, since it's
possible to check for "open" buffers before possibly modifying its contents.
--
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]