(array_type, exporter, importer, batch_factory)
| 317 | |
| 318 | |
| 319 | def check_export_import_batch(array_type, exporter, importer, batch_factory): |
| 320 | c_schema = ffi.new("struct ArrowSchema*") |
| 321 | ptr_schema = int(ffi.cast("uintptr_t", c_schema)) |
| 322 | c_array = ffi.new(f"struct {array_type}*") |
| 323 | ptr_array = int(ffi.cast("uintptr_t", c_array)) |
| 324 | |
| 325 | gc.collect() # Make sure no Arrow data dangles in a ref cycle |
| 326 | old_allocated = pa.total_allocated_bytes() |
| 327 | |
| 328 | # Schema is known up front |
| 329 | batch = batch_factory() |
| 330 | schema = batch.schema |
| 331 | py_value = batch.to_pydict() |
| 332 | exporter(batch, ptr_array) |
| 333 | assert pa.total_allocated_bytes() > old_allocated |
| 334 | # Delete and recreate C++ object from exported pointer |
| 335 | del batch |
| 336 | batch_new = importer(ptr_array, schema) |
| 337 | assert batch_new.to_pydict() == py_value |
| 338 | assert batch_new.schema == schema |
| 339 | assert pa.total_allocated_bytes() > old_allocated |
| 340 | del batch_new, schema |
| 341 | assert pa.total_allocated_bytes() == old_allocated |
| 342 | # Now released |
| 343 | with assert_array_released: |
| 344 | importer(ptr_array, make_schema()) |
| 345 | |
| 346 | # Type is exported and imported at the same time |
| 347 | batch = batch_factory() |
| 348 | py_value = batch.to_pydict() |
| 349 | batch._export_to_c(ptr_array, ptr_schema) |
| 350 | # Delete and recreate C++ objects from exported pointers |
| 351 | del batch |
| 352 | batch_new = importer(ptr_array, ptr_schema) |
| 353 | assert batch_new.to_pydict() == py_value |
| 354 | assert batch_new.schema == batch_factory().schema |
| 355 | assert pa.total_allocated_bytes() > old_allocated |
| 356 | del batch_new |
| 357 | assert pa.total_allocated_bytes() == old_allocated |
| 358 | # Now released |
| 359 | with assert_schema_released: |
| 360 | importer(ptr_array, ptr_schema) |
| 361 | |
| 362 | # Not a struct type |
| 363 | pa.int32()._export_to_c(ptr_schema) |
| 364 | batch_factory()._export_to_c(ptr_array) |
| 365 | with pytest.raises(ValueError, |
| 366 | match="ArrowSchema describes non-struct type"): |
| 367 | importer(ptr_array, ptr_schema) |
| 368 | # Now released |
| 369 | with assert_schema_released: |
| 370 | importer(ptr_array, ptr_schema) |
| 371 | |
| 372 | |
| 373 | @needs_cffi |
no test coverage detected