(array_type, exporter, importer, batch_factory)
| 350 | |
| 351 | |
| 352 | def check_export_import_batch(array_type, exporter, importer, batch_factory): |
| 353 | c_schema = ffi.new("struct ArrowSchema*") |
| 354 | ptr_schema = int(ffi.cast("uintptr_t", c_schema)) |
| 355 | c_array = ffi.new(f"struct {array_type}*") |
| 356 | ptr_array = int(ffi.cast("uintptr_t", c_array)) |
| 357 | |
| 358 | gc.collect() # Make sure no Arrow data dangles in a ref cycle |
| 359 | old_allocated = pa.total_allocated_bytes() |
| 360 | |
| 361 | # Schema is known up front |
| 362 | batch = batch_factory() |
| 363 | schema = batch.schema |
| 364 | py_value = batch.to_pydict() |
| 365 | exporter(batch, ptr_array) |
| 366 | assert pa.total_allocated_bytes() > old_allocated |
| 367 | # Delete and recreate C++ object from exported pointer |
| 368 | del batch |
| 369 | batch_new = importer(ptr_array, schema) |
| 370 | assert batch_new.to_pydict() == py_value |
| 371 | assert batch_new.schema == schema |
| 372 | assert pa.total_allocated_bytes() > old_allocated |
| 373 | del batch_new, schema |
| 374 | assert pa.total_allocated_bytes() == old_allocated |
| 375 | # Now released |
| 376 | with assert_array_released: |
| 377 | importer(ptr_array, make_schema()) |
| 378 | |
| 379 | # Type is exported and imported at the same time |
| 380 | batch = batch_factory() |
| 381 | py_value = batch.to_pydict() |
| 382 | batch._export_to_c(ptr_array, ptr_schema) |
| 383 | # Delete and recreate C++ objects from exported pointers |
| 384 | del batch |
| 385 | batch_new = importer(ptr_array, ptr_schema) |
| 386 | assert batch_new.to_pydict() == py_value |
| 387 | assert batch_new.schema == batch_factory().schema |
| 388 | assert pa.total_allocated_bytes() > old_allocated |
| 389 | del batch_new |
| 390 | assert pa.total_allocated_bytes() == old_allocated |
| 391 | # Now released |
| 392 | with assert_schema_released: |
| 393 | importer(ptr_array, ptr_schema) |
| 394 | |
| 395 | # Not a struct type |
| 396 | pa.int32()._export_to_c(ptr_schema) |
| 397 | batch_factory()._export_to_c(ptr_array) |
| 398 | with pytest.raises(ValueError, |
| 399 | match="ArrowSchema describes non-struct type"): |
| 400 | importer(ptr_array, ptr_schema) |
| 401 | # Now released |
| 402 | with assert_schema_released: |
| 403 | importer(ptr_array, ptr_schema) |
| 404 | |
| 405 | |
| 406 | @needs_cffi |
no test coverage detected