Skip to content

Commit a7b1582

Browse files
committed
test: add tests for arrow_c_stream_capsule destructor behavior
1 parent 11af5fd commit a7b1582

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

python/tests/test_dataframe.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,48 @@ def test_arrow_c_stream_capsule_released(ctx):
17491749
gc.collect()
17501750

17511751

1752+
def test_arrow_c_stream_capsule_clear_destructor(ctx):
1753+
df = ctx.from_pydict({"a": [1]})
1754+
1755+
capsule = df.__arrow_c_stream__()
1756+
1757+
set_destructor = ctypes.pythonapi.PyCapsule_SetDestructor
1758+
set_destructor.restype = ctypes.c_int
1759+
set_destructor.argtypes = [ctypes.py_object, ctypes.c_void_p]
1760+
set_destructor(capsule, ctypes.c_void_p(0))
1761+
1762+
reader = pa.RecordBatchReader._import_from_c_capsule(capsule)
1763+
1764+
del capsule
1765+
gc.collect()
1766+
1767+
table = pa.Table.from_batches(reader)
1768+
expected = pa.table({"a": [1]})
1769+
assert table.equals(expected)
1770+
1771+
1772+
def test_arrow_c_stream_capsule_manual_destructor_noop(ctx):
1773+
df = ctx.from_pydict({"a": [1]})
1774+
1775+
capsule = df.__arrow_c_stream__()
1776+
1777+
get_destructor = ctypes.pythonapi.PyCapsule_GetDestructor
1778+
get_destructor.restype = ctypes.c_void_p
1779+
get_destructor.argtypes = [ctypes.py_object]
1780+
destructor_ptr = get_destructor(capsule)
1781+
destructor = ctypes.CFUNCTYPE(None, ctypes.py_object)(destructor_ptr)
1782+
1783+
reader = pa.RecordBatchReader._import_from_c_capsule(capsule)
1784+
reader.read_all()
1785+
1786+
destructor(capsule)
1787+
destructor(capsule)
1788+
1789+
del reader
1790+
del capsule
1791+
gc.collect()
1792+
1793+
17521794
def test_to_pylist(df):
17531795
# Convert datafusion dataframe to Python list
17541796
pylist = df.to_pylist()

0 commit comments

Comments
 (0)