bpo-36552: Change OverflowError to a ValueError for range objects > PY_SIZE_MAX#12720
Closed
tonybaloney wants to merge 2 commits intopython:masterfrom
Closed
bpo-36552: Change OverflowError to a ValueError for range objects > PY_SIZE_MAX#12720tonybaloney wants to merge 2 commits intopython:masterfrom
tonybaloney wants to merge 2 commits intopython:masterfrom
Conversation
…eError with a different error message
zooba
reviewed
Apr 12, 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)"); |
Member
There was a problem hiding this comment.
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When calculating length of range() objects that have an r->length > PY_SIZE_MAX, the underlying PyLong_AsSsize_t() function will raise an OverflowError:
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.
https://bugs.python.org/issue36552