Skip to content

Commit c08375b

Browse files
Implement PyType_FromSpec & PyType_FromMetaclass (#9213)
* Implement `PyType_FromSpec` & `PyType_FromMetaclass` * Review
1 parent eb2ffc6 commit c08375b

2 files changed

Lines changed: 113 additions & 3 deletions

File tree

‎crates/capi/src/object/pytype.rs‎

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::pystate::with_vm;
55
use crate::slots::{PySlot, PySlotKind, PySlotType};
66
use crate::util::CStrExt;
77
use crate::util::FfiPtrExt;
8-
use core::ffi::{c_char, c_int, c_ulong, c_void};
8+
use core::ffi::{c_char, c_int, c_uint, c_ulong, c_void};
9+
use core::ptr;
910
use rustpython_vm::builtins::{PyStr, PyType};
1011
use rustpython_vm::function::{ItemDoc, PyMethodFlags};
1112
use rustpython_vm::types::{AtomicPyTypeFlags, PyTypeFlags, PyTypeSlots, SlotAccessor};
@@ -22,6 +23,15 @@ pub struct PyType_Slot {
2223
pub pfunc: *mut c_void,
2324
}
2425

26+
#[repr(C)]
27+
pub struct PyType_Spec {
28+
pub name: *const c_char,
29+
pub basicsize: c_int,
30+
pub itemsize: c_int,
31+
pub flags: c_uint,
32+
pub slots: *mut PyType_Slot,
33+
}
34+
2535
impl PyType_Slot {
2636
pub(crate) fn iter<'a>(mut slots: *const Self) -> impl Iterator<Item = &'a Self> {
2737
core::iter::from_fn(move || {
@@ -104,11 +114,11 @@ pub unsafe extern "C" fn PyType_GetFullyQualifiedName(ptr: *mut PyTypeObject) ->
104114

105115
#[unsafe(no_mangle)]
106116
pub unsafe extern "C" fn PyType_GetSlot(_ty: *const PyTypeObject, _slot: c_int) -> *mut c_void {
107-
core::ptr::null_mut()
117+
ptr::null_mut()
108118
}
109119

110120
#[unsafe(no_mangle)]
111-
pub extern "C" fn PyType_FromSlots(slots: *const PySlot) -> *mut PyObject {
121+
pub unsafe extern "C" fn PyType_FromSlots(slots: *const PySlot) -> *mut PyObject {
112122
with_vm(|vm| {
113123
let mut name = None;
114124
let mut base = None;
@@ -254,6 +264,78 @@ pub extern "C" fn PyType_FromSlots(slots: *const PySlot) -> *mut PyObject {
254264
})
255265
}
256266

267+
#[unsafe(no_mangle)]
268+
pub unsafe extern "C" fn PyType_FromMetaclass(
269+
metaclass: *mut PyTypeObject,
270+
module: *mut PyObject,
271+
spec: *mut PyType_Spec,
272+
bases: *mut PyObject,
273+
) -> *mut PyObject {
274+
let &PyType_Spec {
275+
name,
276+
basicsize,
277+
itemsize,
278+
flags,
279+
slots: type_slots,
280+
} = unsafe { &*spec };
281+
let mut slots = [PySlot::SLOT_END; 9];
282+
let mut write_slot = {
283+
let mut iter = slots.iter_mut();
284+
move |slot| {
285+
debug_assert!(
286+
iter.len() >= 2,
287+
"PyType_FromMetaclass: last slot is reserved for SLOT_END"
288+
);
289+
let entry = iter
290+
.next()
291+
.expect("Too many slots for PyType_FromMetaclass");
292+
*entry = slot;
293+
}
294+
};
295+
296+
if !bases.is_null() {
297+
write_slot(PySlot::new(49, bases));
298+
}
299+
300+
if !metaclass.is_null() {
301+
write_slot(PySlot::new(107, metaclass));
302+
}
303+
304+
if !module.is_null() {
305+
write_slot(PySlot::new(108, module));
306+
}
307+
308+
write_slot(PySlot::new(95, name.cast_mut()));
309+
write_slot(PySlot::new(96, basicsize as isize));
310+
write_slot(PySlot::new(98, itemsize as isize));
311+
write_slot(PySlot::new(99, flags as u64));
312+
write_slot(PySlot::new(93, type_slots));
313+
314+
unsafe { PyType_FromSlots(slots.as_ptr()) }
315+
}
316+
317+
#[unsafe(no_mangle)]
318+
pub unsafe extern "C" fn PyType_FromModuleAndSpec(
319+
module: *mut PyObject,
320+
spec: *mut PyType_Spec,
321+
bases: *mut PyObject,
322+
) -> *mut PyObject {
323+
unsafe { PyType_FromMetaclass(ptr::null_mut(), module, spec, bases) }
324+
}
325+
326+
#[unsafe(no_mangle)]
327+
pub unsafe extern "C" fn PyType_FromSpecWithBases(
328+
spec: *mut PyType_Spec,
329+
bases: *mut PyObject,
330+
) -> *mut PyObject {
331+
unsafe { PyType_FromMetaclass(ptr::null_mut(), ptr::null_mut(), spec, bases) }
332+
}
333+
334+
#[unsafe(no_mangle)]
335+
pub unsafe extern "C" fn PyType_FromSpec(spec: *mut PyType_Spec) -> *mut PyObject {
336+
unsafe { PyType_FromMetaclass(ptr::null_mut(), ptr::null_mut(), spec, ptr::null_mut()) }
337+
}
338+
257339
#[unsafe(no_mangle)]
258340
pub unsafe extern "C" fn PyObject_GetTypeData(
259341
obj: *mut PyObject,

‎crates/capi/src/slots.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ impl PySlot {
2020
const SLOT_OPTIONAL: u16 = 0x0001;
2121
const SLOT_STATIC: u16 = 0x0002;
2222
const SLOT_INTPTR: u16 = 0x0004;
23+
pub(crate) const SLOT_END: Self = unsafe { core::mem::zeroed() };
24+
25+
pub(crate) fn new(id: u16, value: impl Into<PySlotValue>) -> Self {
26+
Self {
27+
sl_id: id,
28+
sl_flags: 0,
29+
_reserved: 0,
30+
value: value.into(),
31+
}
32+
}
2333

2434
pub(crate) fn iter<'a>(mut slots: *const Self) -> impl Iterator<Item = &'a Self> {
2535
core::iter::from_fn(move || {
@@ -130,6 +140,24 @@ pub union PySlotValue {
130140
pub sl_uint64: u64,
131141
}
132142

143+
impl<T> From<*mut T> for PySlotValue {
144+
fn from(ptr: *mut T) -> Self {
145+
Self { sl_ptr: ptr.cast() }
146+
}
147+
}
148+
149+
impl From<isize> for PySlotValue {
150+
fn from(size: isize) -> Self {
151+
Self { sl_size: size }
152+
}
153+
}
154+
155+
impl From<u64> for PySlotValue {
156+
fn from(value: u64) -> Self {
157+
Self { sl_uint64: value }
158+
}
159+
}
160+
133161
#[allow(dead_code)]
134162
#[derive(Debug, Copy, Clone)]
135163
pub(crate) enum PySlotModule<'a> {

0 commit comments

Comments
 (0)