Skip to content

Fix setitem/delitem on sequences ignoring the key's __index__ - #1267

Open
jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/operator-setitem-delitem-index
Open

jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/operator-setitem-delitem-index

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

Description

Fixes #1255

PyObjectSetItem and PyObjectDelItem converted an index key only for types with sq_ass_item, so on a type with sequence slots but no sq_ass_item (tuple, str, bytes, range, frozenset, a plain class) they raised TypeError without calling __index__. CPython's PyObject_SetItem and PyObject_DelItem convert the key whenever the type has tp_as_sequence, so an error from __index__, or IndexError for an oversized int, comes first (abstract.c#L237-L252, #L271-L286).

AS-IS

>>> operator.setitem((), RaisingIndex(), 0)   # RaisingIndex.__index__ raises ZeroDivisionError
TypeError: 'tuple(EmptySequenceStorage[])' object does not support item assignment
>>> del ''[RaisingIndex()]
TypeError: '' object does not support item deletion
>>> operator.setitem('', 2**100, 0)
TypeError: '' object does not support item assignment

TO-BE

>>> operator.setitem((), RaisingIndex(), 0)
ZeroDivisionError
>>> del ''[RaisingIndex()]
ZeroDivisionError
>>> operator.setitem('', 2**100, 0)
IndexError: cannot fit 'int' into an index-sized integer

(matching CPython)

Changes

  • Add a doSequenceNoAssItem specialization to PyObjectSetItemGeneric and PyObjectDelItemGeneric for mp_ass_subscript == null, sq_ass_item == null and has_as_sequence(). It converts an index key with PyNumberIndexNode, raises IndexError when the result does not fit in 64 bits, and otherwise raises the existing TypeError. The converted value is not used, so the check follows CPython's 64-bit Py_ssize_t rather than GraalPy's 32-bit index size, and a key such as 2**40 keeps raising TypeError.
  • Add test_setitem_delitem_convert_index_before_type_error to tests/test_operator.py.

Testing

mx python-jvm on darwin-aarch64. tests/test_operator.py passes (4 tests), and the new test fails on the unpatched build. The tagged lib-python suites test_operator, test_index, test_list, test_tuple, test_bytes, test_genericalias, test_types, test_userlist, test_dict, test_array and test_deque pass. Setting and deleting with four keys (a raising __index__, 2**100, 0 and "k") on 34 builtin and user-defined objects now gives the same exception class as CPython 3.13.14, as do the boundary keys 2**31, 2**40, 2**63 - 1, 2**63, -2**63 and -2**63 - 1, apart from itertools.count, which has no sequence slots in GraalPy. Types without sequence slots, such as types.GenericAlias and types.UnionType, still raise TypeError.

PyObjectSetItem and PyObjectDelItem raised TypeError as soon as the
type had neither mp_ass_subscript nor sq_ass_item, without looking at
the key. CPython first converts an index key with
PyNumber_AsSsize_t(key, IndexError) when the type has a sequence
protocol, so an exception from __index__, or IndexError for a key that
does not fit in 64 bits, is raised instead. Add that branch to both
nodes, checking the 64-bit range rather than GraalPy's 32-bit index
size, since the converted value is not used.

Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Oct 31, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

1 participant