GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix `setitem`/`delitem` on sequences ignoring the key's `__index__` by jseop-lim · Pull Request #1267 · oracle/graalpython

Conversation

@jseop-lim

@jseop-lim jseop-lim commented Oct 31, 2026

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jseop-lim
Convert the key before rejecting item assignment on sequences
0392adf
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 <[email protected]>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Oct 31, 2026
msimacek
msimacek reviewed Nov 9, 2026
View reviewed changes
Comment thread graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectDelItem.java
static void doSequenceNoAssItem(VirtualFrame frame, Node inliningTarget, Object object, @SuppressWarnings("unused") TpSlots slots, Object key,
@Cached PyIndexCheckNode indexCheckNode,
@Cached PyNumberIndexNode indexNode) {
if (indexCheckNode.execute(inliningTarget, key) && indexNode.execute(frame, inliningTarget, key) instanceof PInt pint && pint.bitLength() >= Long.SIZE) {

@msimacek msimacek Nov 9, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do the PyNumberAsSsize conversion instead of the PyNumberIndexNode call followed by the PInt check? I don't want to hardcode that PInt is the only possible way to represent a large integer, because we might support native integers.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

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

Reviewers

@msimacek msimacek msimacek left review comments

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[Bug]: Item assignment and deletion on a sequence without sq_ass_item raise TypeError before converting an index key

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@jseop-lim @msimacek
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.