From f5f25ffcabcfe9dbc2640b55dc36776d74133678 Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers Date: Mon, 11 May 2026 13:23:49 +0200 Subject: [PATCH 1/2] Add macro for type check function in c-api --- crates/capi/src/object.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/crates/capi/src/object.rs b/crates/capi/src/object.rs index 47ac58560db..c3ce0a5d023 100644 --- a/crates/capi/src/object.rs +++ b/crates/capi/src/object.rs @@ -7,6 +7,35 @@ use rustpython_vm::{AsObject, Py}; pub type PyTypeObject = Py; +macro_rules! define_py_check { + ($name:ident, $($ctx_path:ident).+) => { + #[unsafe(no_mangle)] + pub unsafe extern "C" fn $name(obj: *mut crate::PyObject) -> core::ffi::c_int { + crate::pystate::with_vm(|vm| unsafe { + obj + .as_ref() + .map(|obj| obj.class().is_subtype(vm.ctx.$($ctx_path).+)) + .unwrap_or_default() + }) + } + }; + (exact $name:ident, $($ctx_path:ident).+) => { + #[unsafe(no_mangle)] + pub unsafe extern "C" fn $name(obj: *mut crate::PyObject) -> core::ffi::c_int { + use rustpython_vm::AsObject; + crate::pystate::with_vm(|vm| unsafe { + obj + .as_ref() + .map(|obj| obj.class().is(vm.ctx.$($ctx_path).+)) + .unwrap_or_default() + }) + } + }; +} + +define_py_check!(PyType_Check, types.type_type); +define_py_check!(exact PyType_CheckExact, types.type_type); + #[unsafe(no_mangle)] pub unsafe extern "C" fn Py_TYPE(op: *mut PyObject) -> *const PyTypeObject { unsafe { (*op).class() } @@ -27,6 +56,15 @@ pub unsafe extern "C" fn PyType_GetFlags(ptr: *const PyTypeObject) -> c_ulong { ty.slots.flags.bits() as u32 as c_ulong } +#[unsafe(no_mangle)] +pub unsafe extern "C" fn PyType_IsSubtype(a: *const PyTypeObject, b: *const PyTypeObject) -> c_int { + with_vm(move |_vm| { + let a = unsafe { &*a }; + let b = unsafe { &*b }; + Ok(a.is_subtype(b)) + }) +} + #[unsafe(no_mangle)] pub extern "C" fn Py_GetConstantBorrowed(constant_id: c_uint) -> *mut PyObject { with_vm(|vm| { From cc1455b74c7ceb8cc7866f5dbf60592f6487689a Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers Date: Sat, 16 May 2026 13:11:28 +0200 Subject: [PATCH 2/2] Add `fn` to macro --- crates/capi/src/object.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/capi/src/object.rs b/crates/capi/src/object.rs index c3ce0a5d023..f71475049f8 100644 --- a/crates/capi/src/object.rs +++ b/crates/capi/src/object.rs @@ -8,7 +8,7 @@ use rustpython_vm::{AsObject, Py}; pub type PyTypeObject = Py; macro_rules! define_py_check { - ($name:ident, $($ctx_path:ident).+) => { + (fn $name:ident, $($ctx_path:ident).+) => { #[unsafe(no_mangle)] pub unsafe extern "C" fn $name(obj: *mut crate::PyObject) -> core::ffi::c_int { crate::pystate::with_vm(|vm| unsafe { @@ -19,7 +19,7 @@ macro_rules! define_py_check { }) } }; - (exact $name:ident, $($ctx_path:ident).+) => { + (exact fn $name:ident, $($ctx_path:ident).+) => { #[unsafe(no_mangle)] pub unsafe extern "C" fn $name(obj: *mut crate::PyObject) -> core::ffi::c_int { use rustpython_vm::AsObject; @@ -33,8 +33,8 @@ macro_rules! define_py_check { }; } -define_py_check!(PyType_Check, types.type_type); -define_py_check!(exact PyType_CheckExact, types.type_type); +define_py_check!(fn PyType_Check, types.type_type); +define_py_check!(exact fn PyType_CheckExact, types.type_type); #[unsafe(no_mangle)] pub unsafe extern "C" fn Py_TYPE(op: *mut PyObject) -> *const PyTypeObject {