Skip to content

[mypyc] Call __index__ when converting objects to u32 and u64 - #22484

Merged
p-sawicki merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-u32-u64-index
Nov 20, 2026
Merged

p-sawicki merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-u32-u64-index

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

Converting an object to u32 or u64 didn't call __index__, unlike every other native int type. The unboxing slow paths CPyLong_AsUInt32_() and CPyLong_AsUInt64_() use PyLong_AsUnsignedLong() and PyLong_AsUnsignedLongLong(), which only accept int objects, while the helpers for u8, u16 and the signed types use PyLong_AsLongAndOverflow(), which calls __index__.

This shows up in three places:

from typing import Any
from mypy_extensions import u16, u64
import librt.strings

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

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

def coerce_u64(o: Any) -> u64:
    x: u64 = o
    return x
u8, u16, i64 etc. u32, u64 before u32, u64 after
take_u64(Index(5)) called from Python 5 TypeError: u64 object expected; got Index 5
coerce_u64(Index(5)) 5 TypeError: an integer is required 5
coerce_u64(1.0) TypeError: 'float' object cannot be interpreted as an integer TypeError: an integer is required same as u16
librt.strings.write_u64_le(w, Index(5)) writes 5 TypeError: an integer is required writes 5

Now the slow paths convert a non-int object with PyNumber_Index() first. The fast path (a one-digit int) isn't changed, and an int still goes straight to PyLong_AsUnsigned*(), so the only extra cost on the slow path for ints is an incref/decref. Negative and too large values (also when returned by __index__) still raise ValueError: int too large or small to convert to u64, and the overlapping error value 239 is handled as before.

Tests: testU32BasicOps and testU64BasicOps now unbox objects with __index__ (including 239 and the maximum value, and -1 and too large values, which raise ValueError), through both an implicit coercion and a call from Python, and check the errors for a float. testLibrtStringsI8AndUnsigned_librt writes Index objects with all write_u16/u32/u64_le/be functions and checks the same errors. The three tests fail without the change to int_ops.c.

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.

@p-sawicki p-sawicki left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@p-sawicki
p-sawicki merged commit a98ea67 into python:master Nov 20, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants