Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ def test_copy():
assert b == b'abc'


def test_bytearray_setitem_converts_value_before_index_check():
import operator
b = bytearray(b'x')
assert_raises(TypeError, operator.setitem, b, 5, 1j)
assert_raises(ValueError, operator.setitem, b, 5, 300)
assert_raises(IndexError, operator.setitem, b, 5, 1)
b[0] = 200
assert b == bytearray(b'\xc8')


def test_setitem():
b = bytearray([1, 2, 3])
b[1] = 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,15 @@ private static PException raiseNonIntIndex(Node inliningTarget, PRaiseNode raise
abstract static class SetItemNode extends SqAssItemBuiltinNode {

@Specialization(guards = "!isNoValue(value)")
static void set(PByteArray self, int index, Object value,
static void set(VirtualFrame frame, PByteArray self, int index, Object value,
@Bind Node inliningTarget,
@Cached CastToByteNode toByteNode,
@Shared @Cached NormalizeIndexWithBoundsCheckNode normalizeIndexNode,
@Cached SequenceStorageNodes.SetItemScalarNode setItemNode) {
// The value is converted first: its __index__ can resize the bytearray.
int byteValue = toByteNode.execute(frame, value) & 0xFF;
index = normalizeIndexNode.execute(index, self.getSequenceStorage().length(), ErrorMessages.BYTEARRAY_OUT_OF_BOUNDS);
setItemNode.execute(inliningTarget, self.getSequenceStorage(), index, value);
setItemNode.execute(inliningTarget, self.getSequenceStorage(), index, byteValue);
}

@Specialization(guards = "isNoValue(value)")
Expand All @@ -250,13 +253,16 @@ static void set(VirtualFrame frame, PByteArray self, Object indexObj, Object val
@Bind Node inliningTarget,
@Cached PyIndexCheckNode indexCheckNode,
@Cached PyNumberAsSizeNode asSizeNode,
@Cached CastToByteNode toByteNode,
@Cached NormalizeIndexWithBoundsCheckNode normalizeIndexNode,
@Cached SequenceStorageNodes.SetItemScalarNode setItemNode,
@Cached @Exclusive PRaiseNode raiseNode) {
if (indexCheckNode.execute(inliningTarget, indexObj)) {
int index = asSizeNode.executeExact(frame, inliningTarget, indexObj);
// The value is converted first: its __index__ can resize the bytearray.
int byteValue = toByteNode.execute(frame, value) & 0xFF;
index = normalizeIndexNode.execute(index, self.getSequenceStorage().length(), ErrorMessages.BYTEARRAY_OUT_OF_BOUNDS);
setItemNode.execute(inliningTarget, self.getSequenceStorage(), index, value);
setItemNode.execute(inliningTarget, self.getSequenceStorage(), index, byteValue);
} else {
throw raiseNode.raise(inliningTarget, TypeError, ErrorMessages.OBJ_INDEX_MUST_BE_INT_OR_SLICES, "bytearray", indexObj);
}
Expand Down