From 933db1075fe9007cf1bf7bda9287ba718c87c806 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Sat, 23 Aug 2025 21:43:35 +0300 Subject: [PATCH] Update `test_long.py` and impl "is_integer" for int --- Lib/test/test_long.py | 56 +++++++++++++++++++++++++++++++++++++----- vm/src/builtins/int.rs | 7 ++++-- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 2a38b133f..3b8690367 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -412,8 +412,7 @@ class LongTest(unittest.TestCase): "Got {}, expected {}.".format(n, actual, expected)) self.assertEqual(actual, expected, msg) - # TODO: RUSTPYTHON - @unittest.expectedFailure + @unittest.expectedFailure # TODO: RUSTPYTHON @support.requires_IEEE_754 def test_float_conversion(self): @@ -824,8 +823,7 @@ class LongTest(unittest.TestCase): self.assertEqual(expected, got, "Incorrectly rounded division {}/{}: " "expected {}, got {}".format(a, b, expected, got)) - # TODO: RUSTPYTHON - @unittest.expectedFailure + @unittest.expectedFailure # TODO: RUSTPYTHON @support.requires_IEEE_754 def test_correctly_rounded_true_division(self): # more stringent tests than those above, checking that the @@ -1344,8 +1342,7 @@ class LongTest(unittest.TestCase): self.assertEqual((0).to_bytes(1, SubStr('big')), b'\x00') self.assertEqual((0).to_bytes(0, SubStr('little')), b'') - # TODO: RUSTPYTHON - @unittest.expectedFailure + @unittest.expectedFailure # TODO: RUSTPYTHON def test_from_bytes(self): def check(tests, byteorder, signed=False): def equivalent_python(byte_array, byteorder, signed=False): @@ -1559,6 +1556,11 @@ class LongTest(unittest.TestCase): b = i.to_bytes(2, signed=True) self.assertIs(int.from_bytes(b, signed=True), i) + def test_is_integer(self): + self.assertTrue((-1).is_integer()) + self.assertTrue((0).is_integer()) + self.assertTrue((1).is_integer()) + def test_access_to_nonexistent_digit_0(self): # http://bugs.python.org/issue14630: A bug in _PyLong_Copy meant that # ob_digit[0] was being incorrectly accessed for instances of a @@ -1602,5 +1604,47 @@ class LongTest(unittest.TestCase): self.assertEqual(n**2, (1 << (2 * bitlen)) - (1 << (bitlen + 1)) + 1) + @unittest.expectedFailure # TODO: RUSTPYTHON + 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 + ) + + # GH-117195 -- This shouldn't crash + object.__sizeof__(1) + if __name__ == "__main__": unittest.main() diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index f93d0f31f..1a4375451 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -686,8 +686,11 @@ impl PyInt { } #[pymethod] - /// Returns the number of ones 1 an int. When the number is < 0, - /// then it returns the number of ones of the absolute value. + const fn is_integer(&self) -> bool { + true + } + + #[pymethod] fn bit_count(&self) -> u32 { self.value.iter_u32_digits().map(|n| n.count_ones()).sum() }