Skip to content

Commit 406ffb5

Browse files
authored
GH-117195: Avoid assertion error in object.__sizeof__ (GH-117220)
1 parent c85e352 commit 406ffb5

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

Lib/test/test_long.py

+2
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,8 @@ class MyInt(int):
16391639
MyInt.__basicsize__ + MyInt.__itemsize__ * ndigits
16401640
)
16411641

1642+
# GH-117195 -- This shouldn't crash
1643+
object.__sizeof__(1)
16421644

16431645
if __name__ == "__main__":
16441646
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid assertion failure for debug builds when calling
2+
``object.__sizeof__(1)``

Objects/typeobject.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -7144,8 +7144,11 @@ object___sizeof___impl(PyObject *self)
71447144

71457145
res = 0;
71467146
isize = Py_TYPE(self)->tp_itemsize;
7147-
if (isize > 0)
7148-
res = Py_SIZE(self) * isize;
7147+
if (isize > 0) {
7148+
/* This assumes that ob_size is valid if tp_itemsize is not 0,
7149+
which isn't true for PyLongObject. */
7150+
res = _PyVarObject_CAST(self)->ob_size * isize;
7151+
}
71497152
res += Py_TYPE(self)->tp_basicsize;
71507153

71517154
return PyLong_FromSsize_t(res);

0 commit comments

Comments
 (0)