From c213a8cc78599d38eb23f470cfe55b61f9a9fb0e Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sun, 15 Nov 2026 03:22:06 +0500 Subject: [PATCH] [mypyc] Call __index__ when converting objects to u32 and u64 The unboxing slow paths for u32 and u64 used PyLong_AsUnsignedLong() and PyLong_AsUnsignedLongLong(), which don't call __index__, unlike the helpers for the other native int types. Convert non-int objects with PyNumber_Index() first. This affects argument unboxing, implicit coercions from Any and the librt.strings write_u32/u64 functions. --- mypyc/lib-rt/int_ops.c | 24 +++++++++++++++++++++-- mypyc/test-data/run-librt-strings.test | 23 ++++++++++++++++++++++ mypyc/test-data/run-u32.test | 27 ++++++++++++++++++++++++++ mypyc/test-data/run-u64.test | 27 ++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) diff --git a/mypyc/lib-rt/int_ops.c b/mypyc/lib-rt/int_ops.c index 9e85519fae338..b00239b1f8b39 100644 --- a/mypyc/lib-rt/int_ops.c +++ b/mypyc/lib-rt/int_ops.c @@ -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(); @@ -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(); diff --git a/mypyc/test-data/run-librt-strings.test b/mypyc/test-data/run-librt-strings.test index e48f506011409..0e703ee5c3617 100644 --- a/mypyc/test-data/run-librt-strings.test +++ b/mypyc/test-data/run-librt-strings.test @@ -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)] diff --git a/mypyc/test-data/run-u32.test b/mypyc/test-data/run-u32.test index 8f7b727aeb122..8346a355c4f7b 100644 --- a/mypyc/test-data/run-u32.test +++ b/mypyc/test-data/run-u32.test @@ -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 diff --git a/mypyc/test-data/run-u64.test b/mypyc/test-data/run-u64.test index 012b26815c47e..aba9c58f7561b 100644 --- a/mypyc/test-data/run-u64.test +++ b/mypyc/test-data/run-u64.test @@ -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