From 88e249bf0048a83e15ac9883737d1abf0ab7cea9 Mon Sep 17 00:00:00 2001 From: ionite34 Date: Sun, 1 Jan 2023 23:08:55 +0800 Subject: [PATCH 1/5] Fix int __sizeof__ to use a minimum absolute ob_size of 1 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. --- Objects/longobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index 0df3b9a9d564e01..6669bc7454bcb52 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5879,7 +5879,7 @@ int___sizeof___impl(PyObject *self) { Py_ssize_t res; - res = offsetof(PyLongObject, ob_digit) + Py_ABS(Py_SIZE(self))*sizeof(digit); + res = offsetof(PyLongObject, ob_digit) + Py_MAX(Py_ABS(Py_SIZE(self)), 1)*sizeof(digit); return res; } From 391ec873612fde1a5ebc8957e527d0b2c59dcf6d Mon Sep 17 00:00:00 2001 From: ionite34 Date: Sun, 1 Jan 2023 23:11:05 +0800 Subject: [PATCH 2/5] Update sizeof test for fix on int 0 --- Lib/test/test_sys.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 17a5026e2571e11..57abe8178a5f215 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1459,7 +1459,7 @@ def get_gen(): yield 1 # listreverseiterator (list) check(reversed([]), size('nP')) # int - check(0, vsize('')) + check(0, vsize('') + self.longdigit) check(1, vsize('') + self.longdigit) check(-1, vsize('') + self.longdigit) PyLong_BASE = 2**sys.int_info.bits_per_digit From 0726feb0a18a61d6233971cbaa45890466ef2eaa Mon Sep 17 00:00:00 2001 From: ionite34 Date: Sun, 1 Jan 2023 23:11:35 +0800 Subject: [PATCH 3/5] Add sizeof test for False to have same size as True --- Lib/test/test_sys.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 57abe8178a5f215..232b79971dc2b79 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1322,6 +1322,7 @@ def test_objecttypes(self): check = self.check_sizeof # bool check(True, vsize('') + self.longdigit) + check(False, vsize('') + self.longdigit) # buffer # XXX # builtin_function_or_method From 7792582a05df3a47ff3c3b00164ce07e50581c77 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 1 Jan 2023 15:59:49 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-01-01-15-59-48.gh-issue-100637.M2n6Kg.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-01-01-15-59-48.gh-issue-100637.M2n6Kg.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-01-01-15-59-48.gh-issue-100637.M2n6Kg.rst b/Misc/NEWS.d/next/Core and Builtins/2023-01-01-15-59-48.gh-issue-100637.M2n6Kg.rst new file mode 100644 index 000000000000000..164f6f5f2f44c0c --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-01-01-15-59-48.gh-issue-100637.M2n6Kg.rst @@ -0,0 +1 @@ +Fix :func:`int.__sizeof__` calculation to include the 1 element ob_digit array for 0 and False. From ab1a78e7f4b93efc1e1c0ac616ce2dc1a6596d0c Mon Sep 17 00:00:00 2001 From: Ionite Date: Mon, 2 Jan 2023 00:54:38 +0800 Subject: [PATCH 5/5] int __sizeof__ format for line length. Add comment on Py_MAX reasoning. Co-authored-by: Mark Dickinson --- Objects/longobject.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index 6669bc7454bcb52..1db4ca418e066ec 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5879,7 +5879,10 @@ int___sizeof___impl(PyObject *self) { Py_ssize_t res; - res = offsetof(PyLongObject, ob_digit) + Py_MAX(Py_ABS(Py_SIZE(self)), 1)*sizeof(digit); + res = offsetof(PyLongObject, ob_digit) + /* using Py_MAX(..., 1) because we always allocate space for at least + one digit, even though the integer zero has a Py_SIZE of 0 */ + + Py_MAX(Py_ABS(Py_SIZE(self)), 1)*sizeof(digit); return res; }