gh-94937: Allow len() to return large Python lengths#150581
Open
cjc0013 wants to merge 1 commit into
Open
Conversation
Documentation build overview
4 files changed± library/functions.html± library/stdtypes.html± reference/datamodel.html± whatsnew/changelog.html |
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.
This implements the
len()fallback discussed in gh-94937 while keeping the existingPy_ssize_tfast path.The change adds an internal object-returning length helper. It first uses
PyObject_Size()for the normal C protocol path, then falls back onOverflowErrorto the type-level__len__special method and validates the result withPyNumber_Index()plus the existing non-negative check.rangenow exposes aMETH_COEXIST__len__method that returns its stored Python integer length, so large ranges can participate in the fallback without changingPyObject_Size()or the C length protocol.The optimized
len()bytecode paths now use the same helper as the builtin, so callers such asrandom.choice()andrandom.sample()see the same behavior.Tests and docs are updated for:
__len__values larger thansys.maxsize__len__rangelengthsrandom.choice()andrandom.sample()over ranges larger thansys.maxsizeTested with:
PYTHONPATH=./Lib:./Modules ./python.exe -m unittest -v test.test_builtin.BuiltinTest.test_len test.test_range.RangeTest.test_large_range test.test_random.MersenneTwister_TestBasicOps.test_choice test.test_random.MersenneTwister_TestBasicOps.test_sample_inputsPYTHONPATH=./Lib:./Modules ./python.exe -m test test_builtin test_range test_random