Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update test_long.py and impl "is_integer" for int
  • Loading branch information
ShaharNaveh committed Aug 23, 2025
commit 933db1075fe9007cf1bf7bda9287ba718c87c806
56 changes: 50 additions & 6 deletions Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,7 @@ def check_float_conversion(self, n):
"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):

Expand Down Expand Up @@ -824,8 +823,7 @@ def check_truediv(self, a, b, skip_small=True):
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
Expand Down Expand Up @@ -1344,8 +1342,7 @@ class SubStr(str):
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):
Expand Down Expand Up @@ -1559,6 +1556,11 @@ def test_from_bytes_small(self):
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
Expand Down Expand Up @@ -1602,5 +1604,47 @@ def test_square(self):
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()
7 changes: 5 additions & 2 deletions vm/src/builtins/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Comment on lines +689 to +692
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Do not expose int.is_integer() to Python

int in CPython does not have an is_integer() method; exposing it breaks compatibility and likely contradicts the updated tests. If you need this helper internally, keep it as a non-#[pymethod] function in a non-pyclass impl block; otherwise, remove it.

Apply this diff to stop exporting it to Python:

-    #[pymethod]
-    const fn is_integer(&self) -> bool {
-        true
-    }

Optionally, if an internal helper is desired elsewhere in the crate, add (outside the #[pyclass] impl):

impl PyInt {
    pub const fn is_integer(&self) -> bool {
        true
    }
}

Run to confirm nothing else exposes is_integer for int:


🏁 Script executed:

#!/bin/bash
# grep for Python-visible int.is_integer definitions/usages
rg -n --type=rust -C2 '(?m)^\s*#\s*\[pymethod\]\s*\n\s*const?\s+fn\s+is_integer\b' vm/src
rg -n --type=python -C2 "hasattr\\([^,]+,\\s*['\\\"]is_integer['\\\"]\\)" Lib/test || true

Length of output: 501


To ensure we didn’t miss any Python-exposed is_integer methods or references, let’s rerun with a corrected regex:


🏁 Script executed:

#!/bin/bash
# Search for any #[pymethod] const fn is_integer in Rust
rg -n -U --type=rust '#\[pymethod\]\s*const\s+fn\s+is_integer' vm/src

# Search for any usage of .is_integer() in Python tests
rg -n --type=python '\.is_integer\s*\(' Lib/test

Length of output: 264


Remove Python exposure of int.is_integer()

The #[pymethod] const fn is_integer(&self) -> bool { … } on lines 688–689 of vm/src/builtins/int.rs is still being exposed to Python. In CPython, int has no is_integer() method, so this breaks compatibility.

Please apply the following diff to stop exporting it:

--- a/vm/src/builtins/int.rs
+++ b/vm/src/builtins/int.rs
@@ impl PyInt {
-    #[pymethod]
-    const fn is_integer(&self) -> bool {
-        true
-    }

If you still need this helper internally, add it outside the #[pyclass] impl as a plain Rust method:

impl PyInt {
    pub const fn is_integer(&self) -> bool {
        true
    }
}
🤖 Prompt for AI Agents
In vm/src/builtins/int.rs around lines 689–692, the const fn is_integer(&self)
is currently exposed to Python via #[pymethod] inside the #[pyclass] impl which
is incompatible with CPython; remove the #[pymethod] export (delete the method
from the pyclass impl) and, if the functionality is still needed internally, add
a plain Rust method on PyInt outside the #[pyclass] impl (e.g., impl PyInt { pub
const fn is_integer(&self) -> bool { true } }) so it is available in Rust but
not exported to Python.

#[pymethod]
fn bit_count(&self) -> u32 {
self.value.iter_u32_digits().map(|n| n.count_ones()).sum()
}
Expand Down