From 34233882cbec4026dac84c7fec4a3bda2779f86e Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 25 Mar 2024 15:07:30 +0000 Subject: [PATCH 1/4] Avoid assertion error in object.__sizeof__ --- .../2024-03-25-15-07-01.gh-issue-117195.OWakgD.rst | 2 ++ Objects/typeobject.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-03-25-15-07-01.gh-issue-117195.OWakgD.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-03-25-15-07-01.gh-issue-117195.OWakgD.rst b/Misc/NEWS.d/next/Core and Builtins/2024-03-25-15-07-01.gh-issue-117195.OWakgD.rst new file mode 100644 index 00000000000000..ae1e5acc5c333b --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-03-25-15-07-01.gh-issue-117195.OWakgD.rst @@ -0,0 +1,2 @@ +Avoid assertion failure for debug builds when calling +``object.__sizeof__(1)`` diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 82822784aaf407..59e12973663d14 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -6810,8 +6810,11 @@ object___sizeof___impl(PyObject *self) res = 0; isize = Py_TYPE(self)->tp_itemsize; - if (isize > 0) - res = Py_SIZE(self) * isize; + if (isize > 0) { + /* This assumes that ob_size is valid if tp_itemsize is not 0, + which isn't true for PyLongObject. */ + res = _PyVarObject_CAST(self)->ob_size * isize; + } res += Py_TYPE(self)->tp_basicsize; return PyLong_FromSsize_t(res); From 7374ca0cbfffbce61907e9ee1fa0d30fe0a006b0 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 25 Mar 2024 15:48:51 +0000 Subject: [PATCH 2/4] Add test --- Lib/test/test_long.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index d299c34cec076d..dda997fda215cc 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1639,6 +1639,8 @@ class MyInt(int): MyInt.__basicsize__ + MyInt.__itemsize__ * ndigits ) + # GC-117195 -- This shouldn't crash + object.__sizeof(1) if __name__ == "__main__": unittest.main() From 4078e91339742e76c4bc8777747537da2a2a7391 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 25 Mar 2024 17:36:27 +0000 Subject: [PATCH 3/4] Fix mangled name --- Lib/test/test_long.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index dda997fda215cc..9e98f7196ac6d1 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1640,7 +1640,7 @@ class MyInt(int): ) # GC-117195 -- This shouldn't crash - object.__sizeof(1) + object.__sizeof__(1) if __name__ == "__main__": unittest.main() From 12f77383381c0a57a93627a00f75a893fabf296d Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Tue, 21 May 2024 19:48:21 +0100 Subject: [PATCH 4/4] Fix typo --- Lib/test/test_long.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 9e98f7196ac6d1..41b973da2c7df0 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1639,7 +1639,7 @@ class MyInt(int): MyInt.__basicsize__ + MyInt.__itemsize__ * ndigits ) - # GC-117195 -- This shouldn't crash + # GH-117195 -- This shouldn't crash object.__sizeof__(1) if __name__ == "__main__":