Skip to content

gh-94937: Allow len() to return large Python lengths#150581

Open
cjc0013 wants to merge 1 commit into
python:mainfrom
cjc0013:gh-94937-len-overflow-fallback
Open

gh-94937: Allow len() to return large Python lengths#150581
cjc0013 wants to merge 1 commit into
python:mainfrom
cjc0013:gh-94937-len-overflow-fallback

Conversation

@cjc0013
Copy link
Copy Markdown

@cjc0013 cjc0013 commented May 29, 2026

This implements the len() fallback discussed in gh-94937 while keeping the existing Py_ssize_t fast path.

The change adds an internal object-returning length helper. It first uses PyObject_Size() for the normal C protocol path, then falls back on OverflowError to the type-level __len__ special method and validates the result with PyNumber_Index() plus the existing non-negative check.

range now exposes a METH_COEXIST __len__ method that returns its stored Python integer length, so large ranges can participate in the fallback without changing PyObject_Size() or the C length protocol.

The optimized len() bytecode paths now use the same helper as the builtin, so callers such as random.choice() and random.sample() see the same behavior.

Tests and docs are updated for:

  • pure Python __len__ values larger than sys.maxsize
  • special-method lookup ignoring instance-level __len__
  • large range lengths
  • random.choice() and random.sample() over ranges larger than sys.maxsize

Tested 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_inputs
  • PYTHONPATH=./Lib:./Modules ./python.exe -m test test_builtin test_range test_random

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented May 29, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32907315 | 📁 Comparing 277eb4f against main (26696a6)

  🔍 Preview build  

4 files changed
± library/functions.html
± library/stdtypes.html
± reference/datamodel.html
± whatsnew/changelog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant