Skip to content

bpo-36552: Change OverflowError to a ValueError for range objects > PY_SIZE_MAX#12720

Closed
tonybaloney wants to merge 2 commits intopython:masterfrom
tonybaloney:overflow_msg
Closed

bpo-36552: Change OverflowError to a ValueError for range objects > PY_SIZE_MAX#12720
tonybaloney wants to merge 2 commits intopython:masterfrom
tonybaloney:overflow_msg

Conversation

@tonybaloney
Copy link
Contributor

@tonybaloney tonybaloney commented Apr 8, 2019

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)

https://bugs.python.org/issue36552

@tonybaloney tonybaloney changed the title bpo-36552: Chagne OverflowError to a ValueError for range objects > PY_SIZE_MAX bpo-36552: Change OverflowError to a ValueError for range objects > PY_SIZE_MAX Apr 8, 2019
Py_ssize_t size = PyLong_AsSsize_t(r->length);
if (size < 0 && PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyErr_Clear();
PyErr_Format(PyExc_ValueError, "Range object too large to calculate length (Overflow Error)");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyErr_Format is only valuable when using format characters :) You want PyErr_SetString here (also your lines are too long for our C style guidelines)

@tonybaloney tonybaloney closed this May 9, 2019
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.

4 participants