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
13 changes: 12 additions & 1 deletion graalpython/com.oracle.graal.python.test/src/tests/test_deque.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -394,6 +394,17 @@ def test_delitem(self):
self.assertTrue(val not in d)
self.assertEqual(len(d), 0)

def test_negative_index_out_of_range(self):
# GH-923: an index < -len must raise IndexError, not wrap around twice
d = deque([10, 20, 30, 40, 50])
i = -len(d) - 1
with self.assertRaises(IndexError):
d[i]
with self.assertRaises(IndexError):
d[i] = 0
with self.assertRaises(IndexError):
del d[i]

def test_reverse(self):
n = 500 # O(n**2) test, don't make this too big
data = [random.random() for i in range(n)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -76,7 +76,6 @@
import com.oracle.graal.python.builtins.PythonBuiltins;
import com.oracle.graal.python.builtins.objects.PNone;
import com.oracle.graal.python.builtins.objects.PNotImplemented;
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexCustomMessageNode;
import com.oracle.graal.python.builtins.objects.deque.DequeBuiltinsClinicProviders.DequeInsertNodeClinicProviderGen;
import com.oracle.graal.python.builtins.objects.deque.DequeBuiltinsClinicProviders.DequeRotateNodeClinicProviderGen;
import com.oracle.graal.python.builtins.objects.function.PKeyword;
Expand Down Expand Up @@ -824,9 +823,12 @@ public abstract static class DequeGetItemNode extends SqItemBuiltinNode {
@Specialization
@TruffleBoundary
Object doGeneric(PDeque self, int idx,
@Cached NormalizeIndexCustomMessageNode normalizeIndexNode) {
int normIdx = normalizeIndexNode.execute(idx, self.getSize(), ErrorMessages.DEQUE_INDEX_OUT_OF_RANGE);
return doGetItem(self, normIdx);
@Bind Node inliningTarget,
@Cached PRaiseNode raiseNode) {
if (idx < 0 || idx >= self.getSize()) {
throw raiseNode.raise(inliningTarget, IndexError, ErrorMessages.DEQUE_INDEX_OUT_OF_RANGE);
}
return doGetItem(self, idx);
}

@TruffleBoundary
Expand All @@ -846,9 +848,12 @@ public abstract static class DequeSetItemNode extends SqAssItemBuiltinNode {

@Specialization
static void setOrDel(PDeque self, int idx, Object value,
@Cached NormalizeIndexCustomMessageNode normalizeIndexNode) {
int normIdx = normalizeIndexNode.execute(idx, self.getSize(), ErrorMessages.DEQUE_INDEX_OUT_OF_RANGE);
self.setItem(normIdx, value != PNone.NO_VALUE ? value : null);
@Bind Node inliningTarget,
@Cached PRaiseNode raiseNode) {
if (idx < 0 || idx >= self.getSize()) {
throw raiseNode.raise(inliningTarget, IndexError, ErrorMessages.DEQUE_INDEX_OUT_OF_RANGE);
}
self.setItem(idx, value != PNone.NO_VALUE ? value : null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Object doGeneric(VirtualFrame frame, Object self, Object index,
CompilerDirectives.transferToInterpreterAndInvalidate();
fixNegativeIndex = insert(FixNegativeIndex.create());
}
size = fixNegativeIndex.execute(frame, size, index);
size = fixNegativeIndex.execute(frame, size, self);
}
return slotNode.execute(frame, self, size);
}
Expand Down Expand Up @@ -210,17 +210,17 @@ Object doGeneric(VirtualFrame frame, Object self, Object index,

@GenerateInline(false) // lazy
public abstract static class FixNegativeIndex extends Node {
public abstract int execute(VirtualFrame frame, int indexAsSize, Object indexObj);
public abstract int execute(VirtualFrame frame, int indexAsSize, Object self);

@Specialization
static int doIt(VirtualFrame frame, int indexAsSize, Object indexObj,
static int doIt(VirtualFrame frame, int indexAsSize, Object self,
@Bind Node inliningTarget,
@Cached GetObjectSlotsNode getIndexSlots,
@Cached GetObjectSlotsNode getSelfSlots,
@Cached CallSlotLenNode callSlotLen) {
assert indexAsSize < 0;
TpSlots indexSlots = getIndexSlots.execute(inliningTarget, indexObj);
if (indexSlots.sq_length() != null) {
int len = callSlotLen.execute(frame, inliningTarget, indexSlots.sq_length(), indexObj);
TpSlots selfSlots = getSelfSlots.execute(inliningTarget, self);
if (selfSlots.sq_length() != null) {
int len = callSlotLen.execute(frame, inliningTarget, selfSlots.sq_length(), self);
return indexAsSize + len;
}
return indexAsSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Object doGeneric(VirtualFrame frame, Object self, Object index, Object value,
CompilerDirectives.transferToInterpreterAndInvalidate();
fixNegativeIndex = insert(FixNegativeIndex.create());
}
size = fixNegativeIndex.execute(frame, size, index);
size = fixNegativeIndex.execute(frame, size, self);
}
slotNode.executeIntKey(frame, self, size, value);
return PNone.NONE;
Expand Down Expand Up @@ -209,7 +209,7 @@ Object doGeneric(VirtualFrame frame, Object self, Object index,
CompilerDirectives.transferToInterpreterAndInvalidate();
fixNegativeIndex = insert(FixNegativeIndex.create());
}
size = fixNegativeIndex.execute(frame, size, index);
size = fixNegativeIndex.execute(frame, size, self);
}
slotNode.executeIntKey(frame, self, size, PNone.NO_VALUE);
return PNone.NONE;
Expand Down