MCPcopy Create free account
hub / github.com/apache/arrow / check_export_import_array

Function check_export_import_array

python/pyarrow/tests/test_cffi.py:184–225  ·  view source on GitHub ↗
(array_type, exporter, importer)

Source from the content-addressed store, hash-verified

182
183
184def check_export_import_array(array_type, exporter, importer):
185 c_schema = ffi.new("struct ArrowSchema*")
186 ptr_schema = int(ffi.cast("uintptr_t", c_schema))
187 c_array = ffi.new(f"struct {array_type}*")
188 ptr_array = int(ffi.cast("uintptr_t", c_array))
189
190 gc.collect() # Make sure no Arrow data dangles in a ref cycle
191 old_allocated = pa.total_allocated_bytes()
192
193 # Type is known up front
194 typ = pa.list_(pa.int32())
195 arr = pa.array([[1], [2, 42]], type=typ)
196 py_value = arr.to_pylist()
197 exporter(arr, ptr_array)
198 assert pa.total_allocated_bytes() > old_allocated
199 # Delete recreate C++ object from exported pointer
200 del arr
201 arr_new = importer(ptr_array, typ)
202 assert arr_new.to_pylist() == py_value
203 assert arr_new.type == pa.list_(pa.int32())
204 assert pa.total_allocated_bytes() > old_allocated
205 del arr_new, typ
206 assert pa.total_allocated_bytes() == old_allocated
207 # Now released
208 with assert_array_released:
209 importer(ptr_array, pa.list_(pa.int32()))
210
211 # Type is exported and imported at the same time
212 arr = pa.array([[1], [2, 42]], type=pa.list_(pa.int32()))
213 py_value = arr.to_pylist()
214 exporter(arr, ptr_array, ptr_schema)
215 # Delete and recreate C++ objects from exported pointers
216 del arr
217 arr_new = importer(ptr_array, ptr_schema)
218 assert arr_new.to_pylist() == py_value
219 assert arr_new.type == pa.list_(pa.int32())
220 assert pa.total_allocated_bytes() > old_allocated
221 del arr_new
222 assert pa.total_allocated_bytes() == old_allocated
223 # Now released
224 with assert_schema_released:
225 importer(ptr_array, ptr_schema)
226
227
228@needs_cffi

Callers 2

test_export_import_arrayFunction · 0.85

Calls 3

newMethod · 0.45
castMethod · 0.45
arrayMethod · 0.45

Tested by

no test coverage detected