-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Closed
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
sys.getsizeof and int#__sizeof__() both underreport the size of the 0 singleton by 4 bytes, due to _PyLong_DIGIT_INIT setting ob_size to 0 when initializing the 0 singleton, the correct value should be 1, since there is a single ob_digit stored, as I understand it.
Examples:
Python 3.10.7 (main, Sep 15 2022, 01:51:29) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getsizeof(0)
24
>>> sys.getsizeof(1)
28Python 3.8.14 (default, Sep 6 2022, 23:26:50)
[Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getsizeof(0)
24
>>> sys.getsizeof(1)
28Details
_PyLong_DIGIT_INIT(val)is used to initialize an immortalPyVarObject.- Depending on the
valpassed it sets the theob_sizefield of the immortal using the expression:((val) == 0 ? 0 : ((val) > 0 ? 1 : -1))
2.1. Whenvalis0, this creates an_PyVarObject_IMMORTAL_INIT(&PyLong_Type, 0), which sets the underlyingPyVarObject'sob_sizeto0. int___sizeof___implusesob_size(throughPy_SIZE) to calculate the size- The
ob_sizeshould be1becauseob_digithas onedigitstored.
4.1. However,ob_sizeis set to0, and this contributes to under-reporting the size of0by thesizeof(digit)which is4
Your environment
- CPython versions tested on:
3.8.14,3.10.7 - Operating system and architecture:
Darwin 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:25:27 PDT 2022; root:xnu-8020.141.5~2/RELEASE_X86_6
Metadata
Metadata
Assignees
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

