paleolimbot commented on code in PR #388: URL: https://github.com/apache/arrow-nanoarrow/pull/388#discussion_r1496004346
########## python/src/nanoarrow/_ipc_lib.pyx: ########## @@ -0,0 +1,145 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# cython: language_level = 3 +# cython: linetrace=True + +from libc.stdint cimport uint8_t, int64_t, uintptr_t +from libc.errno cimport EIO +from libc.stdio cimport snprintf +from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF +from cpython cimport Py_buffer, PyBuffer_FillInfo + +from nanoarrow_c cimport ( + ArrowErrorCode, + ArrowError, + NANOARROW_OK, + ArrowArrayStream, +) + + +cdef extern from "nanoarrow_ipc.h" nogil: + struct ArrowIpcInputStream: + ArrowErrorCode (*read)(ArrowIpcInputStream* stream, uint8_t* buf, + int64_t buf_size_bytes, int64_t* size_read_out, + ArrowError* error) + void (*release)(ArrowIpcInputStream* stream) + void* private_data + + struct ArrowIpcArrayStreamReaderOptions: + int64_t field_index + int use_shared_buffers + + ArrowErrorCode ArrowIpcArrayStreamReaderInit( + ArrowArrayStream* out, ArrowIpcInputStream* input_stream, + ArrowIpcArrayStreamReaderOptions* options) + + +cdef class PyInputStreamPrivate: + cdef object obj + cdef object obj_method + cdef void* addr + cdef Py_ssize_t size_bytes + cdef int close_stream + + def __cinit__(self, obj, close_stream=False): + self.obj = obj + self.obj_method = obj.readinto + self.addr = NULL + self.size_bytes = 0 + self.close_stream = close_stream + + def __getbuffer__(self, Py_buffer* buffer, int flags): + PyBuffer_FillInfo(buffer, self, self.addr, self.size_bytes, 0, flags) + + def __releasebuffer__(self, Py_buffer* buffer): + pass Review Comment: Yes, this is how `readinto()` works. It ensures that there's only ever one copy since the file implementation is often parameterized like this. The underlying `ArrowIpcInputStream` could be parameterized differently to make it avoid a copy using `read() -> bytes`, too (but...battle for another day). -- 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]
