Skip to content

Commit 7ed0583

Browse files
committed
refactor: remove drop_stream function and update capsule management for ArrowArrayStream
1 parent f2e6335 commit 7ed0583

1 file changed

Lines changed: 4 additions & 34 deletions

File tree

src/dataframe.rs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,6 @@ use crate::{
6363
static ARROW_STREAM_NAME: &CStr =
6464
unsafe { CStr::from_bytes_with_nul_unchecked(b"arrow_array_stream\0") };
6565

66-
unsafe extern "C" fn drop_stream(capsule: *mut ffi::PyObject) {
67-
if capsule.is_null() {
68-
return;
69-
}
70-
71-
// When PyArrow imports this capsule it steals the raw stream pointer and
72-
// sets the capsule's internal pointer to NULL. In that case
73-
// `PyCapsule_IsValid` returns 0 and this destructor must not drop the
74-
// stream as ownership has been transferred to PyArrow. If the capsule was
75-
// never imported, the pointer remains valid and we are responsible for
76-
// freeing the stream here.
77-
if ffi::PyCapsule_IsValid(capsule, ARROW_STREAM_NAME.as_ptr()) == 1 {
78-
let stream_ptr = ffi::PyCapsule_GetPointer(capsule, ARROW_STREAM_NAME.as_ptr())
79-
as *mut FFI_ArrowArrayStream;
80-
if !stream_ptr.is_null() {
81-
drop(Box::from_raw(stream_ptr));
82-
}
83-
}
84-
85-
// `PyCapsule_GetPointer` sets a Python error on failure. Clear it only
86-
// after the stream has been released (or determined to be owned
87-
// elsewhere).
88-
ffi::PyErr_Clear();
89-
}
90-
9166
// https://github.com/apache/datafusion-python/pull/1016#discussion_r1983239116
9267
// - we have not decided on the table_provider approach yet
9368
// this is an interim implementation
@@ -998,16 +973,11 @@ impl PyDataFrame {
998973
!stream_ptr.is_null(),
999974
"ArrowArrayStream pointer should never be null",
1000975
);
1001-
// The returned capsule allows zero-copy hand-off to PyArrow. When
1002-
// PyArrow imports the capsule it assumes ownership of the stream and
1003-
// nulls out the capsule's internal pointer so `drop_stream` knows not to
1004-
// free it.
976+
// The returned capsule allows zero-copy hand-off to PyArrow. Once
977+
// PyArrow imports the capsule it assumes ownership of the stream, so we
978+
// rely on PyArrow or PyO3 to manage the stream's lifetime.
1005979
let capsule = unsafe {
1006-
ffi::PyCapsule_New(
1007-
stream_ptr as *mut c_void,
1008-
ARROW_STREAM_NAME.as_ptr(),
1009-
Some(drop_stream),
1010-
)
980+
ffi::PyCapsule_New(stream_ptr as *mut c_void, ARROW_STREAM_NAME.as_ptr(), None)
1011981
};
1012982
if capsule.is_null() {
1013983
unsafe { drop(Box::from_raw(stream_ptr)) };

0 commit comments

Comments
 (0)