Repository navigation
[mypyc] Fix crashes and compile errors from the result of native __len__ methods - #22485
Merged
Merged
Conversation
The sq_length/mp_length slot of a native class returned a negative __len__ result without setting an exception, so len() raised SystemError or crashed. With a bool or native int return type, the slot passed the C value to PyLong_AsSsize_t(), which doesn't compile. With an Any return type, objects with __index__ were rejected. The slot now converts the result like slot_sq_length in CPython, and the compiled len() of a native class instance also raises OverflowError for a length that doesn't fit in Py_ssize_t.
p-sawicki
reviewed
Nov 19, 2026
…pper Move the conversion into CPyTagged_AsLen, which returns the length as a Py_ssize_t (-1 on error). The sq_length/mp_length wrapper calls it instead of duplicating the short int fast path, and CPyTagged_CheckLen (used by compiled len()) is now a thin wrapper around it, so the IR is unchanged.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
sq_length/mp_lengthslot of a native class didn't check the value returned by__len__like CPython'sslot_sq_lengthdoes, and the compiledlen()of a native class instance only partly did:__len__declared to returnintorAnywas returned from the slot as is, without an exception set.len(x)then raisedSystemError: <built-in function len> returned NULL without setting an exceptionwhen called from interpreted code, and crashed the interpreter (segfault) in some other cases, such aslen(x)in compiled code wherexhas typeAny.__len__declared to returnboolor a native int type (i64,i32,i16,i8,u8, ...) generatedPyLong_AsSsize_t(retval)with a C integerretval, which is a C compile error (-Wint-conversion) with current GCC and clang.-> Any, a result that isn't an int raisedTypeError: an integer is required, while Python accepts any object with__index__.len(x), wherexis a native class instance, checked that the length isn't negative, but returned a value that doesn't fit inPy_ssize_t(such as2**63) instead of raisingOverflowError.CPython's
slot_sq_lengthconverts the result with__index__, raisesValueError: __len__() should return >= 0if it's negative, andOverflowError: cannot fit 'int' into an index-sized integerif it doesn't fit inPy_ssize_t.Now:
generate_len_wrapper()handles a short int inline. Anything else goes through the newCPy_LenFromResult()ingeneric_ops.c, which does whatslot_sq_lengthdoes, after boxing the value if needed (likegenerate_hash_wrapper()after [mypyc] Fix OverflowError when a native__hash__returns a large int #22453).len()of a native class instance, the length check is the newCPyTagged_CheckLen()inCPy.h. It's inline for short ints, and for long ints it also raisesOverflowErrorif the value doesn't fit inPy_ssize_t. If__len__returns something other than an int type (for exampleAny), the result is converted byCPyObject_LenFromResult()like in the slot.Tests: the new run test
testDundersLenResultcovers-> int,-> Any,-> bool,-> i64,-> i8and-> u8, through both the compiledlen()and the slot (len()of anAnyvalue), with negative values, values that don't fit inPy_ssize_t,True, an object with__index__and results that aren't ints. I updatedtestDundersLeninirbuild-dunders.testand addedtestDundersLenAny.