You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Item assignment and deletion raise IndexError instead of TypeError on types without them, for a huge index #162466
Assigning to or deleting an item of a tuple, str, bytes, range, set or frozenset raises TypeError, unless the index is an int that doesn't fit in a Py_ssize_t. Then it raises IndexError, although the type supports no item assignment or deletion at all:
s = {1}; s[0] = 0 TypeError: 'set' object does not support item assignment
s = {1}; s[2**63] = 0 IndexError: cannot fit 'int' into an index-sized integer
t = (1, 2); del t[0] TypeError: 'tuple' object doesn't support item deletion
t = (1, 2); del t[2**63] IndexError: cannot fit 'int' into an index-sized integer
A set isn't even subscriptable, and s[2**63] (a read) does give TypeError: 'set' object is not subscriptable. The cause is in PyObject_SetItem and PyObject_DelItem (Objects/abstract.c): when the type has tp_as_sequence, an index key is converted with PyNumber_AsSsize_t(key, PyExc_IndexError) before anyone checks that sq_ass_item exists. The branch for a non-index key already checks sq_ass_item; checking it first in both branches would give the TypeError. PyPy 3.12 raises TypeError in all four cases. I found it while writing a Lean spec of Python.
Activity