Add Py_IncRef, Py_DecRef, Py_XNewRef (#8726)

This commit is contained in:
Bas Schoenmaeckers authored and GitHub committed 2026-09-17 21:09:16 +09:00
1 parent 7da001a7c9
commit da694300b8
1 file changed
+31
+31
View File
@@ -2,12 +2,26 @@ use crate::{PyObject, pystate::with_vm};
use core::ptr::NonNull;
use rustpython_vm::PyObjectRef;
#[unsafe(no_mangle)]
pub unsafe extern "C" fn Py_DecRef(op: *mut PyObject) {
if !op.is_null() {
unsafe { _Py_DecRef(op) }
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn _Py_DecRef(op: *mut PyObject) {
// By dropping PyObjectRef, we will decrement the reference count.
unsafe { drop(PyObjectRef::from_raw(NonNull::new_unchecked(op))) };
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn Py_IncRef(op: *mut PyObject) {
if !op.is_null() {
unsafe { _Py_IncRef(op) }
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn _Py_IncRef(op: *mut PyObject) {
// Don't drop the owned value, as we just want to increment the refcount.
@@ -19,6 +33,15 @@ pub unsafe extern "C" fn Py_NewRef(op: *mut PyObject) -> *mut PyObject {
with_vm(|_vm| unsafe { (*op).to_owned() })
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn Py_XNewRef(op: *mut PyObject) -> *mut PyObject {
if op.is_null() {
core::ptr::null_mut()
} else {
unsafe { Py_NewRef(op) }
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn Py_REFCNT(op: *mut PyObject) -> isize {
with_vm(|_vm| unsafe { &*op }.strong_count())
@@ -45,4 +68,12 @@ mod tests {
assert_eq!(ffi::Py_REFCNT(obj.as_ptr()), ref_count);
});
}
#[test]
fn incdec_null_is_noop() {
unsafe {
ffi::Py_IncRef(core::ptr::null_mut());
ffi::Py_DecRef(core::ptr::null_mut());
}
}
}