]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] GH-117195: Avoid assertion error in `object.__sizeof__` (GH-117220) (#127605)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 11 Dec 2024 11:21:07 +0000 (12:21 +0100)
committerGitHub <noreply@github.com>
Wed, 11 Dec 2024 11:21:07 +0000 (12:21 +0100)
GH-117195: Avoid assertion error in `object.__sizeof__` (GH-117220)
(cherry picked from commit 406ffb5293a8c9ca315bf63de1ee36a9b33f9aaf)

Co-authored-by: Mark Shannon <mark@hotpy.org>
Lib/test/test_long.py
Misc/NEWS.d/next/Core and Builtins/2024-03-25-15-07-01.gh-issue-117195.OWakgD.rst [new file with mode: 0644]
Objects/typeobject.c

index d299c34cec076d0468461e7330b3508e736c1470..41b973da2c7df09c728a3f5c8d6abd3536d2b04b 100644 (file)
@@ -1639,6 +1639,8 @@ class LongTest(unittest.TestCase):
                     MyInt.__basicsize__ + MyInt.__itemsize__ * ndigits
                 )
 
+        # GH-117195 -- This shouldn't crash
+        object.__sizeof__(1)
 
 if __name__ == "__main__":
     unittest.main()
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 (file)
index 0000000..ae1e5ac
--- /dev/null
@@ -0,0 +1,2 @@
+Avoid assertion failure for debug builds when calling
+``object.__sizeof__(1)``
index 5bca4b4e788608d0fd5671d9dc7eb1c5d64de8d4..06857e3685740aaa5b68d2982467cd61e76ec52c 100644 (file)
@@ -6462,8 +6462,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);