[3.11] gh-101266: Fix __sizeof__ for subclasses of int (GH-101394) (#101579)

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.

(Manual backport of #101394 to the Python 3.11 branch.)
This commit is contained in:
Mark Dickinson 2023-02-05 12:18:56 +00:00 committed by GitHub
parent 7cbcfbe2ff
commit cf89c16486
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 9 deletions

View file

@ -1596,5 +1596,44 @@ def test_square(self):
self.assertEqual(n**2,
(1 << (2 * bitlen)) - (1 << (bitlen + 1)) + 1)
def test___sizeof__(self):
self.assertEqual(int.__itemsize__, sys.int_info.sizeof_digit)
# Pairs (test_value, number of allocated digits)
test_values = [
# We always allocate space for at least one digit, even for
# a value of zero; sys.getsizeof should reflect that.
(0, 1),
(1, 1),
(-1, 1),
(BASE-1, 1),
(1-BASE, 1),
(BASE, 2),
(-BASE, 2),
(BASE*BASE - 1, 2),
(BASE*BASE, 3),
]
for value, ndigits in test_values:
with self.subTest(value):
self.assertEqual(
value.__sizeof__(),
int.__basicsize__ + int.__itemsize__ * ndigits
)
# Same test for a subclass of int.
class MyInt(int):
pass
self.assertEqual(MyInt.__itemsize__, sys.int_info.sizeof_digit)
for value, ndigits in test_values:
with self.subTest(value):
self.assertEqual(
MyInt(value).__sizeof__(),
MyInt.__basicsize__ + MyInt.__itemsize__ * ndigits
)
if __name__ == "__main__":
unittest.main()