Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0964f9f

Browse filesBrowse files
[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 406ffb5) Co-authored-by: Mark Shannon <mark@hotpy.org>
1 parent 487a51a commit 0964f9f
Copy full SHA for 0964f9f

File tree

3 files changed

+9
-2
lines changed
Filter options

3 files changed

+9
-2
lines changed

‎Lib/test/test_long.py

Copy file name to clipboardExpand all lines: Lib/test/test_long.py
+2Lines changed: 2 additions & 0 deletions
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()
+2Lines changed: 2 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: Objects/typeobject.c
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6462,8 +6462,11 @@ object___sizeof___impl(PyObject *self)
64626462

64636463
res = 0;
64646464
isize = Py_TYPE(self)->tp_itemsize;
6465-
if (isize > 0)
6466-
res = Py_SIZE(self) * isize;
6465+
if (isize > 0) {
6466+
/* This assumes that ob_size is valid if tp_itemsize is not 0,
6467+
which isn't true for PyLongObject. */
6468+
res = _PyVarObject_CAST(self)->ob_size * isize;
6469+
}
64676470
res += Py_TYPE(self)->tp_basicsize;
64686471

64696472
return PyLong_FromSsize_t(res);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.