File tree 4 files changed +13
-1
lines changed 4 files changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -210,6 +210,7 @@ fn generate_class_def(
210
210
Some ( v) => quote ! ( Some ( #v) ) ,
211
211
None => quote ! ( None ) ,
212
212
} ;
213
+ let basicsize = quote ! ( std:: mem:: size_of:: <#ident>( ) ) ;
213
214
let is_pystruct = attrs. iter ( ) . any ( |attr| {
214
215
path_eq ( & attr. path , "derive" )
215
216
&& if let Ok ( Meta :: List ( l) ) = attr. parse_meta ( ) {
@@ -259,6 +260,7 @@ fn generate_class_def(
259
260
const MODULE_NAME : Option <& ' static str > = #module_name;
260
261
const TP_NAME : & ' static str = #module_class_name;
261
262
const DOC : Option <& ' static str > = #doc;
263
+ const BASICSIZE : usize = #basicsize;
262
264
}
263
265
264
266
impl :: rustpython_vm:: class:: StaticType for #ident {
Original file line number Diff line number Diff line change @@ -326,6 +326,11 @@ impl PyBaseObject {
326
326
fn hash ( zelf : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyHash > {
327
327
Self :: slot_hash ( & zelf, vm)
328
328
}
329
+
330
+ #[ pymethod( magic) ]
331
+ fn sizeof ( zelf : PyObjectRef ) -> usize {
332
+ zelf. class ( ) . slots . basicsize
333
+ }
329
334
}
330
335
331
336
pub fn object_get_dict ( obj : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyDictRef > {
Original file line number Diff line number Diff line change @@ -58,6 +58,7 @@ pub trait PyClassDef {
58
58
const MODULE_NAME : Option < & ' static str > ;
59
59
const TP_NAME : & ' static str ;
60
60
const DOC : Option < & ' static str > = None ;
61
+ const BASICSIZE : usize ;
61
62
}
62
63
63
64
impl < T > PyClassDef for PyRef < T >
68
69
const MODULE_NAME : Option < & ' static str > = T :: MODULE_NAME ;
69
70
const TP_NAME : & ' static str = T :: TP_NAME ;
70
71
const DOC : Option < & ' static str > = T :: DOC ;
72
+ const BASICSIZE : usize = T :: BASICSIZE ;
71
73
}
72
74
73
75
pub trait PyClassImpl : PyClassDef {
@@ -125,6 +127,7 @@ pub trait PyClassImpl: PyClassDef {
125
127
let mut slots = PyTypeSlots {
126
128
flags : Self :: TP_FLAGS ,
127
129
name : PyRwLock :: new ( Some ( Self :: TP_NAME . to_owned ( ) ) ) ,
130
+ basicsize : Self :: BASICSIZE ,
128
131
doc : Self :: DOC ,
129
132
..Default :: default ( )
130
133
} ;
Original file line number Diff line number Diff line change @@ -23,7 +23,9 @@ use std::{
23
23
#[ non_exhaustive]
24
24
pub struct PyTypeSlots {
25
25
pub name : PyRwLock < Option < String > > , // tp_name, not class name
26
- // tp_basicsize, tp_itemsize
26
+
27
+ pub basicsize : usize ,
28
+ // tp_itemsize
27
29
28
30
// Methods to implement standard operations
29
31
You can’t perform that action at this time.
0 commit comments