Skip to content

Commit 9bf9af0

Browse files
committed
Fix a memory leak when using PyList_SET_ITEM on GraalPy
1 parent cb7a413 commit 9bf9af0

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

newsfragments/1.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix memory leak using `PyList_SET_ITEM` on GraalPy

pyo3-ffi/src/cpython/listobject.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ pub unsafe fn PyList_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject
2828

2929
/// Macro, *only* to be used to fill in brand new lists
3030
#[inline]
31-
#[cfg(not(any(PyPy, GraalPy)))]
31+
#[cfg(not(PyPy))]
3232
pub unsafe fn PyList_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) {
33-
*(*(op as *mut PyListObject)).ob_item.offset(i) = v;
33+
#[cfg(not(GraalPy))]
34+
{
35+
*(*(op as *mut PyListObject)).ob_item.offset(i) = v;
36+
}
37+
#[cfg(GraalPy)]
38+
{
39+
let previous = crate::PyList_GetItem(op, i);
40+
Py_XDECREF(previous);
41+
crate::PyList_SetItem(op, i, v);
42+
}
3443
}
3544

3645
#[inline]

pyo3-ffi/src/listobject.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ extern "C" {
6969
#[cfg(PyPy)]
7070
#[cfg_attr(PyPy, link_name = "PyPyList_GET_SIZE")]
7171
pub fn PyList_GET_SIZE(arg1: *mut PyObject) -> Py_ssize_t;
72-
#[cfg(any(PyPy, GraalPy))]
72+
#[cfg(PyPy)]
7373
#[cfg_attr(PyPy, link_name = "PyPyList_SET_ITEM")]
74-
#[cfg_attr(GraalPy, link_name = "_PyList_SET_ITEM")]
7574
pub fn PyList_SET_ITEM(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject);
7675
}

0 commit comments

Comments
 (0)