Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions mypyc/lib-rt/int_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ PyObject *CPyTagged_ToBytes(CPyTagged self, Py_ssize_t length, PyObject *byteord
} else if (strcmp(order, "little") == 0) {
little_endian = 1;
} else {
Py_DECREF(pyint);
PyErr_SetString(PyExc_ValueError, "byteorder must be either 'little' or 'big'");
return NULL;
}
Expand Down
14 changes: 14 additions & 0 deletions mypyc/test-data/run-integers.test
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ class int:
pass

[case testIntToBytes]
import sys
from testutil import assertRaises
def to_bytes(n: int, length: int, byteorder: str, signed: bool = False) -> bytes:
return n.to_bytes(length, byteorder, signed=signed)
Expand All @@ -594,6 +595,19 @@ def test_to_bytes() -> None:
with assertRaises(OverflowError):
to_bytes(128, 1, "big", True)

def test_invalid_byteorder_refcount() -> None:
n = int("123456789012345678901234567890")
before = sys.getrefcount(n)
for _ in range(10):
with assertRaises(ValueError):
to_bytes(n, 16, "invalid")
assert sys.getrefcount(n) == before
# Keep n alive across both refcount checks.
assert n == int("123456789012345678901234567890")

[file sys/__init__.pyi]
def getrefcount(n: object) -> int: ...

[case testBitLength]
def bit_length(n: int) -> int:
return n.bit_length()
Expand Down
Loading