This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author anthony shaw
Recipients anthony shaw
Date 2019-04-08.04:38:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <[email protected]>
In-reply-to
Content
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(2**256))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C ssize_t
>>> a = range(2**256)
>>> len(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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(2**256))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Range object too large to calculate length (Overflow Error)
>>> a = range(2**256)
>>> len(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Range object too large to calculate length (Overflow Error)
History
Date User Action Args
2019-04-08 04:38:16anthony shawsetrecipients: + anthony shaw
2019-04-08 04:38:16anthony shawsetmessageid: <[email protected]>
2019-04-08 04:38:16anthony shawlinkissue36552 messages
2019-04-08 04:38:16anthony shawcreate