mirror of
https://github.com/python/cpython.git
synced 2025-08-09 11:29:45 +00:00
[3.12] GH-117195: Avoid assertion error in object.__sizeof__
(GH-117220) (#127605)
GH-117195: Avoid assertion error in `object.__sizeof__` (GH-117220)
(cherry picked from commit 406ffb5293
)
Co-authored-by: Mark Shannon <mark@hotpy.org>
This commit is contained in:
parent
487a51a1b9
commit
0964f9f184
3 changed files with 9 additions and 2 deletions
|
@ -1639,6 +1639,8 @@ class LongTest(unittest.TestCase):
|
||||||
MyInt.__basicsize__ + MyInt.__itemsize__ * ndigits
|
MyInt.__basicsize__ + MyInt.__itemsize__ * ndigits
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# GH-117195 -- This shouldn't crash
|
||||||
|
object.__sizeof__(1)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Avoid assertion failure for debug builds when calling
|
||||||
|
``object.__sizeof__(1)``
|
|
@ -6462,8 +6462,11 @@ object___sizeof___impl(PyObject *self)
|
||||||
|
|
||||||
res = 0;
|
res = 0;
|
||||||
isize = Py_TYPE(self)->tp_itemsize;
|
isize = Py_TYPE(self)->tp_itemsize;
|
||||||
if (isize > 0)
|
if (isize > 0) {
|
||||||
res = Py_SIZE(self) * isize;
|
/* 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;
|
res += Py_TYPE(self)->tp_basicsize;
|
||||||
|
|
||||||
return PyLong_FromSsize_t(res);
|
return PyLong_FromSsize_t(res);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue