Skip to content

Commit 26f5bbf

Browse files
Add type name c-api functions (#7925)
1 parent ccf1508 commit 26f5bbf

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

crates/capi/src/object.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,33 @@ pub unsafe extern "C" fn PyType_IsSubtype(a: *const PyTypeObject, b: *const PyTy
6666
})
6767
}
6868

69+
#[unsafe(no_mangle)]
70+
pub unsafe extern "C" fn PyType_GetName(ptr: *const PyTypeObject) -> *mut PyObject {
71+
with_vm(|vm| unsafe { &*ptr }.__name__(vm))
72+
}
73+
74+
#[unsafe(no_mangle)]
75+
pub unsafe extern "C" fn PyType_GetQualName(ptr: *const PyTypeObject) -> *mut PyObject {
76+
with_vm(|vm| unsafe { &*ptr }.__qualname__(vm))
77+
}
78+
79+
#[unsafe(no_mangle)]
80+
pub unsafe extern "C" fn PyType_GetFullyQualifiedName(ptr: *const PyTypeObject) -> *mut PyObject {
81+
with_vm(|vm| {
82+
let ty = unsafe { &*ptr };
83+
let qualname = ty.__qualname__(vm).try_downcast::<PyStr>(vm)?;
84+
let module = ty.__module__(vm);
85+
86+
if let Some(module) = module.downcast_ref::<PyStr>()
87+
&& module.as_wtf8() != "builtins"
88+
{
89+
Ok(vm.ctx.new_str(format!("{module}.{qualname}")))
90+
} else {
91+
Ok(qualname)
92+
}
93+
})
94+
}
95+
6996
#[unsafe(no_mangle)]
7097
pub extern "C" fn Py_GetConstantBorrowed(constant_id: c_uint) -> *mut PyObject {
7198
with_vm(|vm| {

0 commit comments

Comments
 (0)