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 on a tuple or str raise TypeError before calling the key's __index__ #9510
Assigning to or deleting an item of a tuple, str, bytes or range raises TypeError in RustPython without calling the key's __index__, but CPython calls __index__ first, so an exception from it propagates.
TypeError: 'tuple' object does not support item assignment
CPython:
ZeroDivisionError
del t[RaisingIndex()], operator.setitem(), operator.delitem() and instances of a class without __setitem__ or __delitem__ diverge the same way.
Root cause
PyObject::set_item and PyObject::del_item convert the key only when the type has an ass_item slot and otherwise raise TypeError straight away. CPython's PyObject_SetItem and PyObject_DelItem convert an __index__ key whenever the type has a sequence table, and only then does PySequence_SetItem or PySequence_DelItem refuse the operation.
Fix
When the type has a sequence table (the has_any() || heaptype_ext() test del_item already uses for its message) and the key has __index__, convert the key to isize before raising the TypeError, raising IndexError for a value that does not fit, as PyNumber_AsSsize_t(key, PyExc_IndexError) does.
Environment
RustPython 713e14c (0.6.1, macOS arm64), also reproduced on 0.4.0
CPython v3.15.2, v3.14.4 and v3.12.10 (macOS arm64)
Python Documentation or reference to CPython source code
AI disclosure: this report was drafted with Claude Code (claude-opus-5-5) and reviewed by me before submission. The divergences were found by differential testing against CPython, checked against CPython 3.15.2, and every source reference above was checked against the linked revisions.
Activity