gh-100637: Fix int and bool __sizeof__ calculation to include the 1 element ob_digit array for 0 and False#100663
Conversation
Fixes behavior where int (and subtypes like bool) __sizeof__ under-reports true size as it did not take into account the size 1 `ob_digit` array for empty ints.
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
mdickinson
left a comment
There was a problem hiding this comment.
This looks good to me, and works as advertised in manual testing - thank you!
I've made one nitpick level suggestion for a stylistic change.
(Actually, thinking about style, I'm not sure why the body of this function doesn't simply consist of a single return - the res temporary seems unnecessary.)
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
mdickinson
left a comment
There was a problem hiding this comment.
LGTM!
I'll leave this open for a bit in case anyone else has comments (especially because it's New Year's Day and some potential commenters may be AFK), but I think this is ready to merge.
Thanks for the review and line split suggestion! I'm not really sure about the |
|
Would this be something that needs backport to 3.10/3.11? Not sure what the policy is for that. |
|
Thanks @ionite34 for the PR, and @mdickinson for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11. |
…he 1 element ob_digit array for 0 and False (pythonGH-100663) Fixes behaviour where int (and subtypes like bool) __sizeof__ under-reports true size as it did not take into account the size 1 `ob_digit` array for the zero int. (cherry picked from commit d7e7f79) Co-authored-by: Ionite <dev@ionite.io> Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
|
GH-100717 is a backport of this pull request to the 3.11 branch. |
It's a judgement call, I think: it's arguably a bugfix, but an almost entirely inconsequential one - I can't imagine much real world code that would benefit, even though it's nice to have the fix going forward. I'll backport to 3.11: Python 3.11 was the version that deliberately started always allocating at least one digit (the relevant commit was 15d50d7). That isn't true in Python 3.10. (3.10 code: Lines 138 to 139 in a639493 (3.11 code: Lines 155 to 162 in b99ac1d |
…the 1 element ob_digit array for 0 and False (GH-100663) (#100717) gh-100637: Fix int and bool __sizeof__ calculation to include the 1 element ob_digit array for 0 and False (GH-100663) Fixes behaviour where int (and subtypes like bool) __sizeof__ under-reports true size as it did not take into account the size 1 `ob_digit` array for the zero int. (cherry picked from commit d7e7f79) Co-authored-by: Ionite <dev@ionite.io> Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Fixes behavior where
intandbool__sizeof__under-reports true size by 4 bytes as it did not take into account the size 1ob_digitarray for0andFalse.Changes
Py_MAXto use the maximum of 1 or the absoluteob_sizesize.Testing
__sizeof__incorrectly ignores the size 1 array in PyVarObjects (bool, int, tuple) when ob_size is 0 #100637