Issue 36552: Replace OverflowError with ValueError when calculating length of range objects > PY_SIZE_MAX (original) (raw)
When calculating length of range() objects that have an r->length > PY_SIZE_MAX, the underlying PyLong_AsSsize_t() function will raise an OverflowError:
a = list(range(2256)) Traceback (most recent call last): File "", line 1, in OverflowError: Python int too large to convert to C ssize_t a = range(2256) len(a) Traceback (most recent call last): File "", line 1, in OverflowError: Python int too large to convert to C ssize_t
This is expected behaviour, but to the average user, who won't know what ssize_t is, or what this has to do with Python int, the user message is confusing and OverflowError is the symptom but not the cause. The cause is that the length sent to range was in a value too large to calculate.
This patch changes OverflowError to ValueError to hint to the user that the value sent to the range object constructor is too large.
a = list(range(2256)) Traceback (most recent call last): File "", line 1, in ValueError: Range object too large to calculate length (Overflow Error) a = range(2256) len(a) Traceback (most recent call last): File "", line 1, in ValueError: Range object too large to calculate length (Overflow Error)
We should at least have consistent error messages:
class O: ... def len(self): ... return 2**100 ... o=O() len(o) Traceback (most recent call last): File "", line 1, in OverflowError: cannot fit 'int' into an index-sized integer
I'd argue for replacing 'int' here with the rendered value, but I think OverflowError is the right type. Mentioning "C ssize_t" is the problem.
As for the list constructor, I'd be okay with chaining a MemoryError here, provided the OverflowError sticks around. But in this context a MemoryError is "recoverable" while an OverflowError very likely indicates a programming error, so we shouldn't hide it from the user.