mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
gh-101266: Fix __sizeof__ for subclasses of int (#101394)
Fix the behaviour of the `__sizeof__` method (and hence the results returned by `sys.getsizeof`) for subclasses of `int`. Previously, `int` subclasses gave identical results to the `int` base class, ignoring the presence of the instance dictionary. <!-- gh-issue-number: gh-101266 --> * Issue: gh-101266 <!-- /gh-issue-number -->
This commit is contained in:
parent
9b60ee976a
commit
39017e04b5
4 changed files with 48 additions and 9 deletions
|
|
@ -5882,13 +5882,10 @@ static Py_ssize_t
|
|||
int___sizeof___impl(PyObject *self)
|
||||
/*[clinic end generated code: output=3303f008eaa6a0a5 input=9b51620c76fc4507]*/
|
||||
{
|
||||
Py_ssize_t res;
|
||||
|
||||
res = offsetof(PyLongObject, long_value.ob_digit)
|
||||
/* using Py_MAX(..., 1) because we always allocate space for at least
|
||||
one digit, even though the integer zero has a Py_SIZE of 0 */
|
||||
+ Py_MAX(Py_ABS(Py_SIZE(self)), 1)*sizeof(digit);
|
||||
return res;
|
||||
/* using Py_MAX(..., 1) because we always allocate space for at least
|
||||
one digit, even though the integer zero has a Py_SIZE of 0 */
|
||||
Py_ssize_t ndigits = Py_MAX(Py_ABS(Py_SIZE(self)), 1);
|
||||
return Py_TYPE(self)->tp_basicsize + Py_TYPE(self)->tp_itemsize * ndigits;
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue