Thanks to visit codestin.com
Credit goes to GitHub.com

Skip to content
Draft
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
3 changes: 2 additions & 1 deletion crates/vm/src/builtins/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ impl PyInt {

#[pymethod]
fn __sizeof__(&self) -> usize {
core::mem::size_of::<Self>() + (((self.value.bits() + 7) & !7) / 8) as usize
let digits = self.value.iter_u32_digits().count().max(1);
core::mem::size_of::<Self>() + (<Self as crate::class::PyClassDef>::ITEMSIZE * digits)
}

#[pymethod]
Expand Down
9 changes: 8 additions & 1 deletion crates/vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{PyDictRef, PyList, PyStr, PyStrRef, PyType, PyTypeRef};
use super::{PyDictRef, PyInt, PyList, PyStr, PyStrRef, PyType, PyTypeRef};
use crate::common::hash::PyHash;
use crate::types::PyTypeFlags;
use crate::{
Expand Down Expand Up @@ -526,6 +526,13 @@ impl PyBaseObject {

#[pymethod]
fn __sizeof__(zelf: PyObjectRef) -> usize {
if let Some(int) = zelf.downcast_ref::<PyInt>() {
let digits = int.as_bigint().iter_u32_digits().count();
let basicsize = zelf.class().slots.basicsize;
let itemsize = zelf.class().slots.itemsize;
return basicsize + itemsize * digits;
}

zelf.class().slots.basicsize
}
}
Expand Down
3 changes: 3 additions & 0 deletions extra_tests/snippets/builtin_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ class MyObject:
0: "ab",
}
assert "ab ab" == "{k[0]} {vv}".format(k=d, vv=d[0])

big = 1223456789812391231291231231231212312312312312312312321321321321312312321123123123199129391239219394923912949213021949302194942130123949203912430392402139210492139123012940219394923942395943856228368385
assert object.__sizeof__(big) >= big.__sizeof__()
Loading