paleolimbot commented on code in PR #318:
URL: https://github.com/apache/arrow-nanoarrow/pull/318#discussion_r1395744762
##########
python/src/nanoarrow/lib.py:
##########
@@ -22,9 +22,10 @@ def schema(obj):
if isinstance(obj, Schema):
return obj
- # Not particularly safe because _export_to_c() could be exporting an
- # array, schema, or array_stream. The ideal
- # solution here would be something like __arrow_c_schema__()
+ if hasattr(obj, "__arrow_c_schema__"):
+ return Schema._import_from_c_capsule(obj.__arrow_c_schema__())
+
+ # for pyarrow < 14.0
Review Comment:
At some point we should maybe remove this logic (or do stricter
check...maybe using `type().__name__` or something). A name-based check would
support older pyarrow, anyway...I think it would be reasonable for any other
package that were using `_export_to_c()` to update and use the new dunder
method to be compatible here.
##########
python/src/nanoarrow/_lib.pyx:
##########
@@ -428,6 +477,33 @@ cdef class Array:
self._ptr = <ArrowArray*>addr
self._schema = schema
+ @staticmethod
+ def _import_from_c_capsule(schema_capsule, array_capsule):
+ """
+ Import from a ArrowSchema and ArrowArray PyCapsule tuple.
+
+ Parameters
+ ----------
+ schema : PyCapsule
+ A valid PyCapsule with name 'arrow_array' containing an
+ ArrowArray pointer.
Review Comment:
This seems like it should be updated?
##########
python/tests/test_capsules.py:
##########
@@ -0,0 +1,81 @@
+# 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 pyarrow as pa
+
+import nanoarrow as na
+
+
+class SchemaWrapper:
+ def __init__(self, schema):
+ self.schema = schema
+
+ def __arrow_c_schema__(self):
+ return self.schema.__arrow_c_schema__()
+
+
+class ArrayWrapper:
Review Comment:
`Array(Stream)Wrapper` should also implement `__arrow_c_schema__`, right?
Even if not used here, it might be good for future readers who end up here to
see the example.
##########
python/src/nanoarrow/_lib.pyx:
##########
@@ -200,6 +227,28 @@ cdef class Schema:
self._base = base,
self._ptr = <ArrowSchema*>addr
+ @staticmethod
+ def _import_from_c_capsule(schema_capsule):
+ """
+ Import from a ArrowSchema PyCapsule
+
+ Parameters
+ ----------
+ schema_capsule : PyCapsule
+ A valid PyCapsule with name 'arrow_schema' containing an
+ ArrowSchema pointer.
+ """
+ cdef:
+ ArrowSchema* c_schema
+ Schema out
+
+ c_schema = <ArrowSchema*> PyCapsule_GetPointer(schema_capsule,
'arrow_schema')
+
+ out = Schema.allocate()
Review Comment:
I wonder if what you want here is more like:
```
cdef Array array = Schema(schema_capsule,
<uintptr_t>PyCapsule_GetPointer(schema_capsule, 'arrow_schema'))
```
(i.e., no need to "move" here...`base` can be a capsule, which does
basically the same thing as the `SchemaHolder`)
##########
python/tests/test_capsules.py:
##########
@@ -0,0 +1,81 @@
+# 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 pyarrow as pa
+
+import nanoarrow as na
+
+
+class SchemaWrapper:
+ def __init__(self, schema):
+ self.schema = schema
+
+ def __arrow_c_schema__(self):
+ return self.schema.__arrow_c_schema__()
+
+
+class ArrayWrapper:
+ def __init__(self, array):
+ self.array = array
+
+ def __arrow_c_array__(self):
+ return self.array.__arrow_c_array__()
+
+
+class StreamWrapper:
+ def __init__(self, stream):
+ self.stream = stream
+
+ def __arrow_c_stream__(self):
Review Comment:
```suggestion
def __arrow_c_stream__(self, schema=None):
```
(right?)
##########
python/tests/test_capsules.py:
##########
@@ -0,0 +1,81 @@
+# 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 pyarrow as pa
+
+import nanoarrow as na
+
+
+class SchemaWrapper:
+ def __init__(self, schema):
+ self.schema = schema
+
+ def __arrow_c_schema__(self):
+ return self.schema.__arrow_c_schema__()
+
+
+class ArrayWrapper:
+ def __init__(self, array):
+ self.array = array
+
+ def __arrow_c_array__(self):
Review Comment:
```suggestion
def __arrow_c_array__(self, schema=None):
```
--
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]