You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
object.__sizeof__(1) have different output between 3.9.10 and 3.12.2 #117195
In 3.11, size of -1000, -1, 0, 1. 1000 are 20, 20, 24, 28, 28. In 3.13, 64, 64, 28, 56 , 56. I am not sure if really a 'bug' rather than intentional, but the only mention of 'size of' in 3.12 What's New is that str object size decreased and no mention that size of ints doubled. I seem's like a regression in memory use. The changelog has several mentions of sizes decreasing and only mentions a stack size increase.
object.__sizeof__(1) gives me 56, but int.__sizeof__(1) and 1 .__sizeof__() and sys.getsizeof(1) all give me 28. And id(1) - id(0) and id(2) - id(1)) both give me 32. So it's certainly only object.__sizeof__ misreporting the size. (In Python 3.12.0.)
Note that the result of object.__sizeof__(1) being an unexpected value is not necessarily a bug, the __sizeof__ method is overridden in int and gives the expected value there. Also as a user the interface to get the size of an object is sys.getsizeof(1).
The crash in debug mode is likely because PyLongObject is not a PyVarObject in 3.12 even though the type has ob_itemsize set to a nonzero value. In 3.9 the type is a PyVarObject, but doesn't follow the protocol entirely because the sign of the int value is stored in ob_itemsize.
I'm not familiar enough with the code in longobject.c and its evolution to know what's the best fix for 3.12. Long term it is probably better to unset ob_itemsize, but who knows what will break when that change is made in 3.12.
Reacted by Kirill Podoprigora and Виталий Дмитриев
Activity
terryjreedy commentedon Mar 24, 2024
In 3.11, size of -1000, -1, 0, 1. 1000 are 20, 20, 24, 28, 28. In 3.13, 64, 64, 28, 56 , 56. I am not sure if really a 'bug' rather than intentional, but the only mention of 'size of' in 3.12 What's New is that str object size decreased and no mention that size of ints doubled. I seem's like a regression in memory use. The changelog has several mentions of sizes decreasing and only mentions a stack size increase.
pochmann3 commentedon Mar 24, 2024
object.__sizeof__(1)gives me 56, butint.__sizeof__(1)and1 .__sizeof__()andsys.getsizeof(1)all give me 28. Andid(1) - id(0)andid(2) - id(1))both give me 32. So it's certainly onlyobject.__sizeof__misreporting the size. (In Python 3.12.0.)Eclips4 commentedon Mar 24, 2024
It's ends up with an assertion error on debug build for me (current main branch):
vitaldmit commentedon Mar 24, 2024
Eclips4 commentedon Mar 24, 2024
Is this a debug build? For the debug build, you should use
./configure --with-pydebugon Linux orpcbuild/build.bat -c Debugon Windowsronaldoussoren commentedon Mar 24, 2024
Note that the result of
object.__sizeof__(1)being an unexpected value is not necessarily a bug, the__sizeof__method is overridden inintand gives the expected value there. Also as a user the interface to get the size of an object issys.getsizeof(1).The crash in debug mode is likely because PyLongObject is not a PyVarObject in 3.12 even though the type has
ob_itemsizeset to a nonzero value. In 3.9 the type is a PyVarObject, but doesn't follow the protocol entirely because the sign of theintvalue is stored inob_itemsize.I'm not familiar enough with the code in longobject.c and its evolution to know what's the best fix for 3.12. Long term it is probably better to unset
ob_itemsize, but who knows what will break when that change is made in 3.12.Eclips4 commentedon Mar 24, 2024
Perhaps @markshannon is the right person for this discussion.
markshannon commentedon Mar 25, 2024
The result of
object.__sizeof__(1)is not meaningful, but it shouldn't crash.GH-117195: Avoid assertion error in `object.__sizeof__` (GH-117220)
pythonGH-117195: Avoid assertion error in `object.__sizeof__` (python…
[3.13] GH-117195: Avoid assertion error in `object.__sizeof__` (GH-11…
pythonGH-117195: Avoid assertion error in `object.__sizeof__` (python…
pythonGH-117195: Avoid assertion error in `object.__sizeof__` (python…
[3.12] GH-117195: Avoid assertion error in `object.__sizeof__` (GH-11…