Repository navigation
[mypyc] Raise IndexError for indexes that don't fit in Py_ssize_t - #22477
Open
Jahongir-Qurbonov wants to merge 1 commit into
Open
Jahongir-Qurbonov wants to merge 1 commit into
Jahongir-Qurbonov wants to merge 1 commit into
Conversation
CPython raises IndexError("cannot fit 'int' into an index-sized
integer") when indexing a list, tuple, str or bytes object with an int
that doesn't fit in Py_ssize_t. Compiled code raised OverflowError
instead from the lib-rt item access primitives, and ValueError ("int
too large to convert to i64") from the specialized bytes[i], ord(s[i])
and s[i] == "x" code. The lib-rt primitives also raised OverflowError
for ints that fit in Py_ssize_t but aren't short ints (e.g. 2**62),
which CPython treats as just out of range.
Set the right IndexError in the non-short int slow paths of the list,
tuple, str and bytes item access primitives. In the specialized code,
use a slow path primitive that raises IndexError when coercing a
non-short int index to i64. The fast path for short ints is unchanged.
This also fixes a compile error for an index literal that doesn't fit
in i64 (e.g. b[2**63]).
list.pop() and list.insert() now only raise OverflowError if the index
doesn't fit in Py_ssize_t, like CPython: other non-short indexes are
out of range for pop() and clamped for insert().
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.
CPython raises
IndexError: cannot fit 'int' into an index-sized integerwhen a list, tuple, str or bytes object is indexed with an int that doesn't fit inPy_ssize_t. Compiled code raised other exceptions:OverflowError: Python int too large to convert to C ssize_tfrom the list, tuple and str item access primitives (get, set and borrowed get).ValueError: int too large to convert to i64from the specializedb[i],ord(s[i])ands[i] == "x"code (and BytesWriter/StringWriter indexing). A literal index such asb[2**63]was even a compile error.Py_ssize_tbut aren't short ints (e.g.2**62), which CPython treats as just out of range ("list index out of range" etc.).list.pop()andlist.insert()had the same problem: CPython raisesIndexError: pop index out of rangeand clamps the index, respectively.The slow paths for non-short int indexes now use
PyNumber_AsSsize_t(index, PyExc_IndexError), like CPython. Values that fit fall through to the usual "index out of range" error. The specialized code uses a new slow-path primitive when coercing a non-short int index to i64; the fast path for short ints doesn't change.list.pop()andlist.insert()only raise OverflowError when the index doesn't fit inPy_ssize_t, as in CPython.Slicing and bytearray indexing already matched CPython.
This touches the same
run-u64.testlines as #22471 (theOverflowErrorassertions fortuple/strindexes with largeu64values); I'll resolve the conflict in whichever PR lands second.