(schema_factory, expected_schema_factory=None)
| 287 | |
| 288 | |
| 289 | def check_export_import_schema(schema_factory, expected_schema_factory=None): |
| 290 | if expected_schema_factory is None: |
| 291 | expected_schema_factory = schema_factory |
| 292 | |
| 293 | c_schema = ffi.new("struct ArrowSchema*") |
| 294 | ptr_schema = int(ffi.cast("uintptr_t", c_schema)) |
| 295 | |
| 296 | gc.collect() # Make sure no Arrow data dangles in a ref cycle |
| 297 | old_allocated = pa.total_allocated_bytes() |
| 298 | |
| 299 | schema_factory()._export_to_c(ptr_schema) |
| 300 | assert pa.total_allocated_bytes() > old_allocated |
| 301 | # Delete and recreate C++ object from exported pointer |
| 302 | schema_new = pa.Schema._import_from_c(ptr_schema) |
| 303 | assert schema_new == expected_schema_factory() |
| 304 | assert pa.total_allocated_bytes() == old_allocated |
| 305 | del schema_new |
| 306 | assert pa.total_allocated_bytes() == old_allocated |
| 307 | # Now released |
| 308 | with assert_schema_released: |
| 309 | pa.Schema._import_from_c(ptr_schema) |
| 310 | |
| 311 | # Not a struct type |
| 312 | pa.int32()._export_to_c(ptr_schema) |
| 313 | with pytest.raises(ValueError, |
| 314 | match="ArrowSchema describes non-struct type"): |
| 315 | pa.Schema._import_from_c(ptr_schema) |
| 316 | # Now released |
| 317 | with assert_schema_released: |
| 318 | pa.Schema._import_from_c(ptr_schema) |
| 319 | |
| 320 | |
| 321 | @needs_cffi |
no test coverage detected