Repository navigation
[mypyc] Call __index__ when converting objects to u32 and u64 - #22484
Merged
Merged
Conversation
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.
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.
Converting an object to
u32oru64didn't call__index__, unlike every other native int type. The unboxing slow pathsCPyLong_AsUInt32_()andCPyLong_AsUInt64_()usePyLong_AsUnsignedLong()andPyLong_AsUnsignedLongLong(), which only accept int objects, while the helpers foru8,u16and the signed types usePyLong_AsLongAndOverflow(), which calls__index__.This shows up in three places:
u8,u16,i64etc.u32,u64beforeu32,u64aftertake_u64(Index(5))called from Python5TypeError: u64 object expected; got Index5coerce_u64(Index(5))5TypeError: an integer is required5coerce_u64(1.0)TypeError: 'float' object cannot be interpreted as an integerTypeError: an integer is requiredu16librt.strings.write_u64_le(w, Index(5))5TypeError: an integer is required5Now 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 toPyLong_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 raiseValueError: int too large or small to convert to u64, and the overlapping error value 239 is handled as before.Tests:
testU32BasicOpsandtestU64BasicOpsnow unbox objects with__index__(including 239 and the maximum value, and -1 and too large values, which raiseValueError), through both an implicit coercion and a call from Python, and check the errors for a float.testLibrtStringsI8AndUnsigned_librtwritesIndexobjects with allwrite_u16/u32/u64_le/befunctions and checks the same errors. The three tests fail without the change toint_ops.c.