Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add weakref check
  • Loading branch information
youknowone committed Jan 16, 2026
commit 3277a4db8be7a9de28907fe2c27b74a8e665bc20
14 changes: 14 additions & 0 deletions crates/vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ fn object_getstate_default(obj: &PyObject, required: bool, vm: &VirtualMachine)
basicsize += core::mem::size_of::<PyObjectRef>();
}

// Add __weakref__ size if type has weakref support
let has_weakref = if let Some(ref ext) = obj.class().heaptype_ext {
match &ext.slots {
None => true, // Heap type without __slots__ has automatic weakref
Some(slots) => slots.iter().any(|s| s.as_str() == "__weakref__"),
}
} else {
let weakref_name = vm.ctx.intern_str("__weakref__");
obj.class().attributes.read().contains_key(weakref_name)
};
if has_weakref {
basicsize += core::mem::size_of::<PyObjectRef>();
}

// Add slots size
if let Some(ref slot_names) = slot_names {
basicsize += core::mem::size_of::<PyObjectRef>() * slot_names.__len__();
Expand Down