forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
py/objint: Fix int.to_bytes() buffer size checks.
Fixes and improvements to `int.to_bytes()` are: - No longer overflows if byte size is 0 (closes micropython#13041). - Raises OverflowError in any case where number won't fit into byte length (now matches CPython, previously MicroPython would return a truncated bytes object). - Document that `micropython int.to_bytes()` doesn't implement the optional signed kwarg, but will behave as if `signed=True` when the integer is negative (this is the current behaviour). Add tests for this also. Requires changes for small ints, MPZ large ints, and "long long" large ints. Adds a new set of unit tests for ints between 32 and 64 bits to increase coverage of "long long" large ints, which are otherwise untested. Tested on unix port (64 bit small ints, MPZ long ints) and Zephyr STM32WB board (32 bit small ints, long long large ints). This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
- Loading branch information
1 parent
d933210
commit 908ab1c
Showing
12 changed files
with
302 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import sys | ||
|
||
# Depending on the port, the numbers in this test may be implemented as "small" | ||
# native 64 bit ints, arbitrary precision large ints, or large integers using 64-bit | ||
# long longs. | ||
|
||
try: | ||
x = int.from_bytes(b"\x6F\xAB\xCD\x12\x34\x56\x78\xFB", "big") | ||
except OverflowError: | ||
print("SKIP") # Port can't represent this size of integer at all | ||
raise SystemExit | ||
|
||
print(hex(x)) | ||
b = x.to_bytes(8, "little") | ||
print(b) | ||
print(x.to_bytes(8, "big")) | ||
|
||
# padding in output | ||
print(x.to_bytes(20, "little")) | ||
print(x.to_bytes(20, "big")) | ||
|
||
# check that extra zero bytes don't change the internal int value | ||
print(int.from_bytes(b + bytes(10), "little") == x) | ||
|
||
# can't write to a zero-length bytes object | ||
try: | ||
x.to_bytes(0, "little") | ||
except OverflowError: | ||
print("OverflowError") | ||
|
||
# or one that it too short | ||
try: | ||
x.to_bytes(7, "big") | ||
except OverflowError: | ||
print("OverflowError") | ||
|
||
# negative representations | ||
|
||
# MicroPython int.to_bytes() behaves as if signed=True for negative numbers | ||
if "micropython" in repr(sys.implementation): | ||
|
||
def to_bytes_compat(i, l, e): | ||
return i.to_bytes(l, e) | ||
else: | ||
# Implement MicroPython compatible behaviour for CPython | ||
def to_bytes_compat(i, l, e): | ||
return i.to_bytes(l, e, signed=i < 0) | ||
|
||
|
||
print(to_bytes_compat(-x, 8, "little")) | ||
print(to_bytes_compat(-x, 20, "big")) | ||
print(to_bytes_compat(-x, 20, "little")) |
Oops, something went wrong.