jorisvandenbossche commented on code in PR #388:
URL: https://github.com/apache/arrow-nanoarrow/pull/388#discussion_r1495829216


##########
.github/workflows/python-wheels.yaml:
##########
@@ -99,11 +99,8 @@ jobs:
           python -m cibuildwheel --output-dir wheelhouse python
         env:
           CIBW_ARCHS_MACOS: x86_64 arm64
-          # Optional (test suite will pass if these are not available)
-          # Commenting this for now because not all the tests pass yet (fixes 
in another PR)
-          # CIBW_BEFORE_TEST: pip install --only-binary ":all:" pyarrow numpy 
|| pip install --only-binary ":all:" numpy || true

Review Comment:
   Not related to this PR, but it might be good to test the wheels with those 
dependencies?



##########
python/src/nanoarrow/ipc.py:
##########
@@ -0,0 +1,184 @@
+# 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.
+
+import io
+
+from nanoarrow._ipc_lib import CIpcInputStream, init_array_stream
+from nanoarrow._lib import CArrayStream
+
+
+class Stream:
+    """Stream of serialized Arrow data
+
+    Reads file paths or otherwise readable file objects that contain
+    serialized Arrow data. Arrow documentation typically refers to this format
+    as "Arrow IPC" because its origin was as a means to transmit tables between
+    processes; however, this format can also be written to and read from files
+    or URLs and is essentially a high-performance equivalent of a CSV file that
+    does a better job maintaining type fidelity.
+
+    Use :staticmethod:`from_readable`, :staticmethod:`from_path`, or
+    :staticmethod:`from_url` to construct these streams.
+    """
+
+    def __init__(self):
+        self._stream = None
+        self._desc = None
+
+    def _is_valid(self) -> bool:
+        return self._stream is not None and self._stream.is_valid()
+
+    def __arrow_c_stream__(self, requested_schema=None):
+        """Export this stream as an ArrowArrayStream
+
+        Implements the Arrow PyCapsule interface by transferring ownership of 
this
+        input stream to an ArrowArrayStream wrapped by a PyCapsule.
+        """
+        if not self._is_valid():
+            raise RuntimeError("nanoarrow.ipc.Stream is no longer valid")
+
+        array_stream = CArrayStream.allocate()
+        init_array_stream(self._stream, array_stream._addr())
+        return 
array_stream.__arrow_c_stream__(requested_schema=requested_schema)
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, *args, **kwargs):
+        if self._stream is not None:
+            self._stream.release()
+
+    @staticmethod
+    def from_readable(obj):
+        """Wrap an open readable object as an Arrow stream
+
+        Wraps a readable object (specificially, an object that implements a
+        ``readinto()`` method) as a non-owning Stream. Closing ``obj`` remains
+        the caller's responsibility: neither this stream nor the resulting 
array
+        stream will call ``obj.close()``.
+
+        Parameters
+        ----------
+        obj : readable file-like
+            An object implementing ``readinto()``.
+        """
+        out = Stream()
+        out._stream = CIpcInputStream.from_readable(obj)
+        out._desc = repr(obj)
+        return out
+
+    @staticmethod
+    def from_path(obj, *args, **kwargs):
+        """Wrap an open readable object as an Arrow stream

Review Comment:
   ```suggestion
           """Wrap a local file as an Arrow stream
   ```



##########
python/src/nanoarrow/_lib.pyx:
##########
@@ -1926,6 +1926,10 @@ cdef class CArrayStream:
         self._ptr = <ArrowArrayStream*>addr
         self._cached_schema = None
 
+    def release(self):

Review Comment:
   Typically, for python objects with such a context manager, such method would 
often be called `close()`. Although I think here we might indeed want to stick 
to the terminology of the C Data Interface?



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

Review Comment:
   Side question: this being an int, does that mean you can only either read 
all fields or either a single field?



##########
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:
   Why are those needed? Is that needed to make `obj_method` work?



##########
python/src/nanoarrow/ipc.py:
##########
@@ -0,0 +1,184 @@
+# 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.
+
+import io
+
+from nanoarrow._ipc_lib import CIpcInputStream, init_array_stream
+from nanoarrow._lib import CArrayStream
+
+
+class Stream:
+    """Stream of serialized Arrow data
+
+    Reads file paths or otherwise readable file objects that contain
+    serialized Arrow data. Arrow documentation typically refers to this format
+    as "Arrow IPC" because its origin was as a means to transmit tables between

Review Comment:
   And further, this is specifically for the streaming format, not the file 
format? 



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