Skip to content

Fix bytearray item assignment checking the index before the value - #1355

Open
jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/bytearray-setitem-value-first
Open

jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/bytearray-setitem-value-first

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

Description

Part of #1291 (item 10).

ByteArrayBuiltins bounds-checks the index before it converts the value, in both SetItemNode and SetSubscriptNode. CPython converts the value first (bytearray_setitem, bytearray_ass_subscript).

AS-IS

>>> b = bytearray(b'x')
>>> b[5] = 1j
IndexError: bytearray index out of range
>>> b[5] = 300
IndexError: bytearray index out of range

TO-BE

>>> b[5] = 1j
TypeError: an integer is required (got type complex)
>>> b[5] = 300
ValueError: byte must be in range(0, 256)

(matching CPython's exception types; the TypeError message differs as it did before)

Changes

  • Convert the value with CastToByteNode before normalizing the index in both nodes.
  • Add a test to tests/test_bytes.py.

Testing

mx python-jvm on darwin-aarch64, then mx graalpytest on test_bytes.py (the new test fails on the unpatched build and passes with the fix) and the tagged test_bytes (288 passed). mx python-checkcopyrights, mx eclipseformat and mx checkstyle are clean.

b[5] = 1j on a one-byte bytearray raised IndexError. CPython converts
the value first and raises TypeError or ValueError for it.

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 Nov 25, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant