Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1158,8 +1158,9 @@ are always available. They are listed here in alphabetical order.

.. impl-detail::

``len`` raises :exc:`OverflowError` on lengths larger than
:data:`sys.maxsize`, such as :class:`range(2 ** 100) <range>`.
CPython's C length protocol is limited to :data:`sys.maxsize`.
When that limit is reached, ``len`` may still return a larger Python
integer if the object's :meth:`~object.__len__` method can provide one.


.. _func-list:
Expand Down
5 changes: 3 additions & 2 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1533,8 +1533,9 @@ loops.
indices.

Ranges containing absolute values larger than :data:`sys.maxsize` are
permitted but some features (such as :func:`len`) may raise
:exc:`OverflowError`.
permitted, though some operations that use the C length protocol may still
raise :exc:`OverflowError` for ranges with lengths larger than
:data:`!sys.maxsize`.

Range examples::

Expand Down
9 changes: 4 additions & 5 deletions Doc/reference/datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3226,11 +3226,10 @@ through the object's keys; for sequences, it should iterate through the values.

.. impl-detail::

In CPython, the length is required to be at most :data:`sys.maxsize`.
If the length is larger than :data:`!sys.maxsize` some features (such as
:func:`len`) may raise :exc:`OverflowError`. To prevent raising
:exc:`!OverflowError` by truth value testing, an object must define a
:meth:`~object.__bool__` method.
In CPython, the C length protocol is limited to :data:`sys.maxsize`.
Some features that use that protocol may raise :exc:`OverflowError`
for larger lengths. To prevent raising :exc:`!OverflowError` by truth
value testing, an object must define a :meth:`~object.__bool__` method.


.. method:: object.__length_hint__(self)
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ PyAPI_FUNC(PyObject *) _PyNumber_PowerNoMod(PyObject *lhs, PyObject *rhs);
PyAPI_FUNC(PyObject *) _PyNumber_InPlacePowerNoMod(PyObject *lhs, PyObject *rhs);

PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
PyAPI_FUNC(PyObject *) _PyObject_LengthAsPyLong(PyObject *o);

/* === Sequence protocol ================================================ */

Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,10 @@ def __len__(self):
class HugeLen:
def __len__(self):
return sys.maxsize + 1
self.assertRaises(OverflowError, len, HugeLen())
self.assertEqual(len(HugeLen()), sys.maxsize + 1)
huge_len = HugeLen()
huge_len.__len__ = lambda: 0
self.assertEqual(len(huge_len), sys.maxsize + 1)
class HugeNegativeLen:
def __len__(self):
return -sys.maxsize-10
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
import pickle
import shlex
import sys
import warnings
import test.support

Expand Down Expand Up @@ -122,6 +123,7 @@ def test_choice(self):
choice([])
self.assertEqual(choice([50]), 50)
self.assertIn(choice([25, 75]), [25, 75])
self.assertIn(choice(range(sys.maxsize * 2)), range(sys.maxsize * 2))

def test_choice_with_numpy(self):
# Accommodation for NumPy arrays which have disabled __bool__().
Expand Down Expand Up @@ -177,6 +179,11 @@ def test_sample_inputs(self):
self.gen.sample(range(20), 2)
self.gen.sample(str('abcdefghijklmnopqrst'), 2)
self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)
population = range(sys.maxsize * 2)
sample = self.gen.sample(population, 10)
self.assertEqual(len(sample), 10)
self.assertEqual(len(set(sample)), 10)
self.assertTrue(all(element in population for element in sample))

def test_sample_on_dicts(self):
self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2)
Expand Down
21 changes: 4 additions & 17 deletions Lib/test/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,14 @@ def test_large_operands(self):

def test_large_range(self):
# Check long ranges (len > sys.maxsize)
# len() is expected to fail due to limitations of the __len__ protocol
def _range_len(x):
try:
length = len(x)
except OverflowError:
step = x[1] - x[0]
length = 1 + ((x[-1] - x[0]) // step)
return length

a = -sys.maxsize
b = sys.maxsize
expected_len = b - a
x = range(a, b)
self.assertIn(a, x)
self.assertNotIn(b, x)
self.assertRaises(OverflowError, len, x)
self.assertEqual(len(x), expected_len)
self.assertTrue(x)
self.assertEqual(_range_len(x), expected_len)
self.assertEqual(x[0], a)
idx = sys.maxsize+1
self.assertEqual(x[idx], a+idx)
Expand All @@ -195,9 +185,8 @@ def _range_len(x):
x = range(a, b)
self.assertIn(a, x)
self.assertNotIn(b, x)
self.assertRaises(OverflowError, len, x)
self.assertEqual(len(x), expected_len)
self.assertTrue(x)
self.assertEqual(_range_len(x), expected_len)
self.assertEqual(x[0], a)
idx = sys.maxsize+1
self.assertEqual(x[idx], a+idx)
Expand All @@ -214,9 +203,8 @@ def _range_len(x):
x = range(a, b, c)
self.assertIn(a, x)
self.assertNotIn(b, x)
self.assertRaises(OverflowError, len, x)
self.assertEqual(len(x), expected_len)
self.assertTrue(x)
self.assertEqual(_range_len(x), expected_len)
self.assertEqual(x[0], a)
idx = sys.maxsize+1
self.assertEqual(x[idx], a+(idx*c))
Expand All @@ -233,9 +221,8 @@ def _range_len(x):
x = range(a, b, c)
self.assertIn(a, x)
self.assertNotIn(b, x)
self.assertRaises(OverflowError, len, x)
self.assertEqual(len(x), expected_len)
self.assertTrue(x)
self.assertEqual(_range_len(x), expected_len)
self.assertEqual(x[0], a)
idx = sys.maxsize+1
self.assertEqual(x[idx], a+(idx*c))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Allow :func:`len` to return values larger than :data:`sys.maxsize` when
``__len__`` can provide a Python integer fallback, including for large
:class:`range` objects.
13 changes: 2 additions & 11 deletions Modules/_testinternalcapi/test_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,48 @@ PyObject_Length(PyObject *o)
}
#define PyObject_Length PyObject_Size

PyObject *
_PyObject_LengthAsPyLong(PyObject *o)
{
Py_ssize_t res = PyObject_Size(o);
if (res >= 0) {
return PyLong_FromSsize_t(res);
}
assert(PyErr_Occurred());
if (!PyErr_ExceptionMatches(PyExc_OverflowError)) {
return NULL;
}

PyErr_Clear();
PyObject *meth = _PyObject_LookupSpecial(o, &_Py_ID(__len__));
if (meth == NULL) {
if (!PyErr_Occurred()) {
type_error("object of type '%.200s' has no len()", o);
}
return NULL;
}

PyObject *len = _PyObject_CallNoArgs(meth);
Py_DECREF(meth);
if (len == NULL) {
return NULL;
}

Py_SETREF(len, PyNumber_Index(len));
if (len == NULL) {
return NULL;
}

assert(PyLong_Check(len));
if (_PyLong_IsNegative((PyLongObject *)len)) {
Py_DECREF(len);
PyErr_SetString(PyExc_ValueError,
"__len__() should return >= 0");
return NULL;
}
return len;
}

int
_PyObject_HasLen(PyObject *o) {
return (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_length) ||
Expand Down
8 changes: 8 additions & 0 deletions Objects/rangeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ range_length(PyObject *op)
return PyLong_AsSsize_t(r->length);
}

static PyObject *
range_len(PyObject *op, PyObject *Py_UNUSED(ignored))
{
rangeobject *r = (rangeobject*)op;
return Py_NewRef(r->length);
}

static PyObject *
compute_item(rangeobject *r, PyObject *i)
{
Expand Down Expand Up @@ -779,6 +786,7 @@ PyDoc_STRVAR(index_doc,
"Raise ValueError if the value is not present.");

static PyMethodDef range_methods[] = {
{"__len__", range_len, METH_NOARGS | METH_COEXIST, NULL},
{"__reversed__", range_reverse, METH_NOARGS, reverse_doc},
{"__reduce__", range_reduce, METH_NOARGS},
{"count", range_count, METH_O, count_doc},
Expand Down
10 changes: 2 additions & 8 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Built-in functions */

#include "Python.h"
#include "pycore_abstract.h" // _PyObject_LengthAsPyLong()
#include "pycore_ast.h" // _PyAST_Validate()
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_cell.h" // PyCell_GetRef()
Expand Down Expand Up @@ -1992,14 +1993,7 @@ static PyObject *
builtin_len(PyObject *module, PyObject *obj)
/*[clinic end generated code: output=fa7a270d314dfb6c input=bc55598da9e9c9b5]*/
{
Py_ssize_t res;

res = PyObject_Size(obj);
if (res < 0) {
assert(PyErr_Occurred());
return NULL;
}
return PyLong_FromSsize_t(res);
return _PyObject_LengthAsPyLong(obj);
}


Expand Down
13 changes: 3 additions & 10 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// See Tools/cases_generator/README.md for more information.

#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_abstract.h" // _PyIndex_Check(), _PyObject_LengthAsPyLong()
#include "pycore_audit.h" // _PySys_Audit()
#include "pycore_backoff.h"
#include "pycore_cell.h" // PyCell_GetRef()
Expand Down Expand Up @@ -3698,9 +3698,7 @@ dummy_func(

inst(GET_LEN, (obj -- obj, len)) {
// PUSH(len(TOS))
Py_ssize_t len_i = PyObject_Length(PyStackRef_AsPyObjectBorrow(obj));
ERROR_IF(len_i < 0);
PyObject *len_o = PyLong_FromSsize_t(len_i);
PyObject *len_o = _PyObject_LengthAsPyLong(PyStackRef_AsPyObjectBorrow(obj));
ERROR_IF(len_o == NULL);
len = PyStackRef_FromPyObjectSteal(len_o);
}
Expand Down Expand Up @@ -5037,12 +5035,7 @@ dummy_func(
/* len(o) */
STAT_INC(CALL, hit);
PyObject *arg_o = PyStackRef_AsPyObjectBorrow(arg);
Py_ssize_t len_i = PyObject_Length(arg_o);
if (len_i < 0) {
ERROR_NO_POP();
}
PyObject *res_o = PyLong_FromSsize_t(len_i);
assert((res_o != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
PyObject *res_o = _PyObject_LengthAsPyLong(arg_o);
if (res_o == NULL) {
ERROR_NO_POP();
}
Expand Down
2 changes: 1 addition & 1 deletion Python/ceval.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define _PY_INTERPRETER

#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_abstract.h" // _PyIndex_Check(), _PyObject_LengthAsPyLong()
#include "pycore_audit.h" // _PySys_Audit()
#include "pycore_backoff.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
Expand Down
15 changes: 2 additions & 13 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Tools/jit/template.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Python.h"

#include "pycore_abstract.h"
#include "pycore_backoff.h"
#include "pycore_call.h"
#include "pycore_cell.h"
Expand Down
Loading