Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions crates/capi/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@ use rustpython_vm::{AsObject, Py};

pub type PyTypeObject = Py<PyType>;

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);
Comment on lines +35 to +36
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

I prefer to contains fn, struct, impl in macro to help searching fn PyType_Check. Otherwise the result will be zero.


#[unsafe(no_mangle)]
pub unsafe extern "C" fn Py_TYPE(op: *mut PyObject) -> *const PyTypeObject {
unsafe { (*op).class() }
Expand All @@ -26,6 +55,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| {
Expand Down
Loading