Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions derive/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ fn generate_class_def(
Some(v) => quote!(Some(#v) ),
None => quote!(None),
};
let basicsize = quote!(std::mem::size_of::<#ident>());
let is_pystruct = attrs.iter().any(|attr| {
path_eq(&attr.path, "derive")
&& if let Ok(Meta::List(l)) = attr.parse_meta() {
Expand Down Expand Up @@ -259,6 +260,7 @@ fn generate_class_def(
const MODULE_NAME: Option<&'static str> = #module_name;
const TP_NAME: &'static str = #module_class_name;
const DOC: Option<&'static str> = #doc;
const BASICSIZE: usize = #basicsize;
}

impl ::rustpython_vm::class::StaticType for #ident {
Expand Down
5 changes: 5 additions & 0 deletions vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ impl PyBaseObject {
fn hash(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyHash> {
Self::slot_hash(&zelf, vm)
}

#[pymethod(magic)]
fn sizeof(zelf: PyObjectRef) -> usize {
zelf.class().slots.basicsize
}
}

pub fn object_get_dict(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyDictRef> {
Expand Down
3 changes: 3 additions & 0 deletions vm/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub trait PyClassDef {
const MODULE_NAME: Option<&'static str>;
const TP_NAME: &'static str;
const DOC: Option<&'static str> = None;
const BASICSIZE: usize;
}

impl<T> PyClassDef for PyRef<T>
Expand All @@ -68,6 +69,7 @@ where
const MODULE_NAME: Option<&'static str> = T::MODULE_NAME;
const TP_NAME: &'static str = T::TP_NAME;
const DOC: Option<&'static str> = T::DOC;
const BASICSIZE: usize = T::BASICSIZE;
}

pub trait PyClassImpl: PyClassDef {
Expand Down Expand Up @@ -125,6 +127,7 @@ pub trait PyClassImpl: PyClassDef {
let mut slots = PyTypeSlots {
flags: Self::TP_FLAGS,
name: PyRwLock::new(Some(Self::TP_NAME.to_owned())),
basicsize: Self::BASICSIZE,
doc: Self::DOC,
..Default::default()
};
Expand Down
4 changes: 3 additions & 1 deletion vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use std::{
#[non_exhaustive]
pub struct PyTypeSlots {
pub name: PyRwLock<Option<String>>, // tp_name, not class name
// tp_basicsize, tp_itemsize

pub basicsize: usize,
// tp_itemsize

// Methods to implement standard operations

Expand Down