diff options
| author | Angus Gratton <angus@redyak.com.au> | 2025-03-26 11:07:52 +1100 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-07-18 00:12:05 +1000 |
| commit | 516aa02104c3344903bdda078b7c87f71f94938d (patch) | |
| tree | db852578d823aab814afbf05124dfea8e0b9c464 /tests | |
| parent | d07f103d68d8bb1b65cba047b9bef093b9375ebd (diff) | |
py/objint_longlong: Add arithmetic overflow checks.
Long long big integer support now raises an exception on overflow rather
than returning an undefined result.
Also adds an error when shifting by a negative value.
The new arithmetic checks are added in the misc.h header.
This work was funded through GitHub Sponsors.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/basics/int_64_basics.py | 13 | ||||
| -rw-r--r-- | tests/extmod/uctypes_addressof.py | 7 |
2 files changed, 17 insertions, 3 deletions
diff --git a/tests/basics/int_64_basics.py b/tests/basics/int_64_basics.py index 73a06b64b..289ea49b6 100644 --- a/tests/basics/int_64_basics.py +++ b/tests/basics/int_64_basics.py @@ -117,10 +117,21 @@ x = -4611686018427387904 # big # sys.maxsize is a constant bigint, so test it's compatible with dynamic ones import sys if hasattr(sys, "maxsize"): - print(sys.maxsize + 1 - 1 == sys.maxsize) + print(sys.maxsize - 1 + 1 == sys.maxsize) else: print(True) # No maxsize property in this config # test extraction of big int value via mp_obj_get_int_maybe x = 1 << 62 print('a' * (x + 4 - x)) + +# negative shifts are invalid +try: + print((1 << 48) >> -4) +except ValueError as e: + print(e) + +try: + print((1 << 48) << -6) +except ValueError as e: + print(e) diff --git a/tests/extmod/uctypes_addressof.py b/tests/extmod/uctypes_addressof.py index c83089d0f..213fcc05e 100644 --- a/tests/extmod/uctypes_addressof.py +++ b/tests/extmod/uctypes_addressof.py @@ -12,5 +12,8 @@ for i in range(8): print(uctypes.addressof(uctypes.bytearray_at(1 << i, 8))) # Test address that is bigger than the greatest small-int but still within the address range. -large_addr = maxsize + 1 -print(uctypes.addressof(uctypes.bytearray_at(large_addr, 8)) == large_addr) +try: + large_addr = maxsize + 1 + print(uctypes.addressof(uctypes.bytearray_at(large_addr, 8)) == large_addr) +except OverflowError: + print(True) # systems with 64-bit bigints will overflow on the above operation |
