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
24 changes: 22 additions & 2 deletions mypyc/lib-rt/int_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,24 @@ void CPyInt16_Overflow() {
PyErr_SetString(PyExc_ValueError, "int too large to convert to i16");
}

// Unlike PyLong_AsLongAndOverflow(), which the other native int types use,
// PyLong_AsUnsignedLong() and PyLong_AsUnsignedLongLong() don't call __index__, so
// convert the object first. Return a new reference, or NULL with an exception set.
static PyObject *CPyLong_IndexForUnsigned(PyObject *o) {
if (likely(PyLong_Check(o))) {
return Py_NewRef(o);
}
return PyNumber_Index(o);
}

// u64 unboxing slow path
uint64_t CPyLong_AsUInt64_(PyObject *o) {
unsigned long long result = PyLong_AsUnsignedLongLong(o);
PyObject *index = CPyLong_IndexForUnsigned(o);
if (index == NULL) {
return CPY_LL_UINT_ERROR;
}
unsigned long long result = PyLong_AsUnsignedLongLong(index);
Py_DECREF(index);
if (result == (unsigned long long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyErr_Clear();
Expand All @@ -663,7 +678,12 @@ void CPyUInt64_Overflow() {

// u32 unboxing slow path
uint32_t CPyLong_AsUInt32_(PyObject *o) {
unsigned long result = PyLong_AsUnsignedLong(o);
PyObject *index = CPyLong_IndexForUnsigned(o);
if (index == NULL) {
return CPY_LL_UINT_ERROR;
}
unsigned long result = PyLong_AsUnsignedLong(index);
Py_DECREF(index);
if (result == (unsigned long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyErr_Clear();
Expand Down
23 changes: 23 additions & 0 deletions mypyc/test-data/run-librt-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,29 @@ def test_write_via_any() -> None:
with assertRaises(TypeError):
write_func(w)

class Index:
def __init__(self, n: int) -> None:
self.n = n

def __index__(self) -> int:
return self.n

def test_write_via_any_index_and_non_int() -> None:
for write_func, fmt, bits in WRITE_FUNCS:
values = [0, 5, 239, (1 << bits) - 1]
w: Any = BytesWriter()
for v in values:
write_func(w, Index(v))
assert w.getvalue() == b"".join(struct.pack(fmt, v) for v in values)

msg = f"int too large or small to convert to u{bits}"
with assertRaises(ValueError, msg):
write_func(w, Index(1 << bits))
with assertRaises(ValueError, msg):
write_func(w, Index(-1))
with assertRaises(TypeError, "'float' object cannot be interpreted as an integer"):
write_func(w, 1.0)

def test_read_via_any() -> None:
for read_func, fmt, bits in READ_FUNCS:
values = [0, 239, (1 << bits) - 1, 1 << (bits - 1)]
Expand Down
27 changes: 27 additions & 0 deletions mypyc/test-data/run-u32.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ def test_box_and_unbox() -> None:
o = -1
x3: u32 = o

class Index:
def __init__(self, n: int) -> None:
self.n = n

def __index__(self) -> int:
return self.n

def take_u32(x: u32) -> u32:
return x

def test_unbox_index_and_non_int() -> None:
f: Any = take_u32
for n in [0, 5, ERROR, 2**30, 4294967295]:
o: Any = Index(n)
x: u32 = o
assert x == n
assert f(o) == n
for n in [-1, 4294967296, 18446744073709551616]:
o = Index(n)
with assertRaises(ValueError, "int too large or small to convert to u32"):
x = o
o = 1.0
with assertRaises(TypeError, "'float' object cannot be interpreted as an integer"):
x = o
with assertRaises(TypeError, "u32 object expected; got float"):
f(o)

def div_by_7(x: u32) -> u32:
return x // 7

Expand Down
27 changes: 27 additions & 0 deletions mypyc/test-data/run-u64.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ def test_box_and_unbox() -> None:
o = -1
x3: u64 = o

class Index:
def __init__(self, n: int) -> None:
self.n = n

def __index__(self) -> int:
return self.n

def take_u64(x: u64) -> u64:
return x

def test_unbox_index_and_non_int() -> None:
f: Any = take_u64
for n in [0, 5, ERROR, 2**40, 18446744073709551615]:
o: Any = Index(n)
x: u64 = o
assert x == n
assert f(o) == n
for n in [-1, 18446744073709551616]:
o = Index(n)
with assertRaises(ValueError, "int too large or small to convert to u64"):
x = o
o = 1.0
with assertRaises(TypeError, "'float' object cannot be interpreted as an integer"):
x = o
with assertRaises(TypeError, "u64 object expected; got float"):
f(o)

def div_by_7(x: u64) -> u64:
return x // 7

Expand Down
Loading