Add type name c-api functions (#7925)

This commit is contained in:
Bas Schoenmaeckers authored and GitHub committed 2026-05-19 21:03:39 +09:00
1 parent 1e20975838
commit 720791c58e
1 file changed
+27
+27
View File
@@ -66,6 +66,33 @@ pub unsafe extern "C" fn PyType_IsSubtype(a: *const PyTypeObject, b: *const PyTy
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyType_GetName(ptr: *const PyTypeObject) -> *mut PyObject {
with_vm(|vm| unsafe { &*ptr }.__name__(vm))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyType_GetQualName(ptr: *const PyTypeObject) -> *mut PyObject {
with_vm(|vm| unsafe { &*ptr }.__qualname__(vm))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyType_GetFullyQualifiedName(ptr: *const PyTypeObject) -> *mut PyObject {
with_vm(|vm| {
let ty = unsafe { &*ptr };
let qualname = ty.__qualname__(vm).try_downcast::<PyStr>(vm)?;
let module = ty.__module__(vm);
if let Some(module) = module.downcast_ref::<PyStr>()
&& module.as_wtf8() != "builtins"
{
Ok(vm.ctx.new_str(format!("{module}.{qualname}")))
} else {
Ok(qualname)
}
})
}
#[unsafe(no_mangle)]
pub extern "C" fn Py_GetConstantBorrowed(constant_id: c_uint) -> *mut PyObject {
with_vm(|vm| {