paleolimbot commented on code in PR #378: URL: https://github.com/apache/arrow-nanoarrow/pull/378#discussion_r1483614582
########## 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: Agreed! `data` was the name of the `ArrowBuffer` C struct field but we've departed pretty significantly from it here, so definitely worth renaming (or maybe better: just implement the buffer protocol on the buffer as you suggested below!) -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org