Skip to content

Commit d86c071

Browse files
committed
Externalize normalized index error messages
1 parent ba5e61f commit d86c071

File tree

6 files changed

+33
-34
lines changed

6 files changed

+33
-34
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,14 +1803,14 @@ public PSequence rangeStartStopStep(Object cls, Object start, Object stop, Objec
18031803
}
18041804
}
18051805

1806-
throw raise(TypeError, ErrorMessages.RAGE_DOES_NOT_SUPPORT, start, stop, step);
1806+
throw raise(TypeError, ErrorMessages.RANGE_DOES_NOT_SUPPORT, start, stop, step);
18071807
}
18081808

18091809
@TruffleBoundary
18101810
@Specialization(guards = "!isNumber(stop)")
18111811
public PSequence rangeError(Object cls, Object start, Object stop, Object step) {
18121812
CompilerDirectives.transferToInterpreter();
1813-
throw raise(TypeError, ErrorMessages.RAGE_DOES_NOT_SUPPORT, start, stop, step);
1813+
throw raise(TypeError, ErrorMessages.RANGE_DOES_NOT_SUPPORT, start, stop, step);
18141814
}
18151815

18161816
public static boolean isNumber(Object value) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PythonCextBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2609,7 +2609,7 @@ Object doPTuple(VirtualFrame frame, PTuple tuple, long key,
26092609
SequenceStorage sequenceStorage = tuple.getSequenceStorage();
26102610
// we must do a bounds-check but we must not normalize the index
26112611
if (key < 0 || key >= lenNode.execute(sequenceStorage)) {
2612-
throw raise(IndexError, NormalizeIndexNode.TUPLE_OUT_OF_BOUNDS);
2612+
throw raise(IndexError, ErrorMessages.TUPLE_OUT_OF_BOUNDS);
26132613
}
26142614
return getItemNode.execute(frame, sequenceStorage, key);
26152615
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/IndexNodes.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.oracle.graal.python.builtins.objects.common.IndexNodesFactory.NormalizeIndexWithBoundsCheckNodeGen;
4646
import com.oracle.graal.python.builtins.objects.common.IndexNodesFactory.NormalizeIndexWithoutBoundsCheckNodeGen;
4747
import com.oracle.graal.python.builtins.objects.ints.PInt;
48+
import com.oracle.graal.python.nodes.ErrorMessages;
4849
import com.oracle.graal.python.nodes.PRaiseNode;
4950
import com.oracle.graal.python.util.OverflowException;
5051
import com.oracle.truffle.api.dsl.Cached;
@@ -57,15 +58,6 @@
5758
public abstract class IndexNodes {
5859

5960
public static final class NormalizeIndexNode extends Node {
60-
public static final String INDEX_OUT_OF_BOUNDS = "index out of range";
61-
public static final String RANGE_OUT_OF_BOUNDS = "range index out of range";
62-
public static final String TUPLE_OUT_OF_BOUNDS = "tuple index out of range";
63-
public static final String TUPLE_ASSIGN_OUT_OF_BOUNDS = "tuple assignment index out of range";
64-
public static final String LIST_OUT_OF_BOUNDS = "list index out of range";
65-
public static final String LIST_ASSIGN_OUT_OF_BOUNDS = "list assignment index out of range";
66-
public static final String ARRAY_OUT_OF_BOUNDS = "array index out of range";
67-
public static final String ARRAY_ASSIGN_OUT_OF_BOUNDS = "array assignment index out of range";
68-
public static final String BYTEARRAY_OUT_OF_BOUNDS = "bytearray index out of range";
6961

7062
@Child private NormalizeIndexCustomMessageNode subNode;
7163

@@ -101,51 +93,51 @@ protected final String getErrorMessage() {
10193
}
10294

10395
public static NormalizeIndexNode create() {
104-
return new NormalizeIndexNode(INDEX_OUT_OF_BOUNDS, true);
96+
return new NormalizeIndexNode(ErrorMessages.INDEX_OUT_OF_RANGE, true);
10597
}
10698

10799
public static NormalizeIndexNode create(String errorMessage) {
108100
return new NormalizeIndexNode(errorMessage, true);
109101
}
110102

111103
public static NormalizeIndexNode create(boolean boundsCheck) {
112-
return new NormalizeIndexNode(INDEX_OUT_OF_BOUNDS, boundsCheck);
104+
return new NormalizeIndexNode(ErrorMessages.INDEX_OUT_OF_RANGE, boundsCheck);
113105
}
114106

115107
public static NormalizeIndexNode create(String errorMessage, boolean boundsCheck) {
116108
return new NormalizeIndexNode(errorMessage, boundsCheck);
117109
}
118110

119111
public static NormalizeIndexNode forList() {
120-
return create(LIST_OUT_OF_BOUNDS);
112+
return create(ErrorMessages.LIST_INDEX_OUT_OF_RANGE);
121113
}
122114

123115
public static NormalizeIndexNode forListAssign() {
124-
return create(LIST_ASSIGN_OUT_OF_BOUNDS);
116+
return create(ErrorMessages.LIST_ASSIGMENT_INDEX_OUT_OF_RANGE);
125117
}
126118

127119
public static NormalizeIndexNode forTuple() {
128-
return create(TUPLE_OUT_OF_BOUNDS);
120+
return create(ErrorMessages.TUPLE_OUT_OF_BOUNDS);
129121
}
130122

131123
public static NormalizeIndexNode forTupleAssign() {
132-
return create(TUPLE_ASSIGN_OUT_OF_BOUNDS);
124+
return create(ErrorMessages.TUPLE_ASSIGN_OUT_OF_BOUNDS);
133125
}
134126

135127
public static NormalizeIndexNode forArray() {
136-
return create(ARRAY_OUT_OF_BOUNDS);
128+
return create(ErrorMessages.ARRAY_OUT_OF_BOUNDS);
137129
}
138130

139131
public static NormalizeIndexNode forArrayAssign() {
140-
return create(ARRAY_ASSIGN_OUT_OF_BOUNDS);
132+
return create(ErrorMessages.ARRAY_ASSIGN_OUT_OF_BOUNDS);
141133
}
142134

143135
public static NormalizeIndexNode forRange() {
144-
return create(RANGE_OUT_OF_BOUNDS);
136+
return create(ErrorMessages.RANGE_OUT_OF_BOUNDS);
145137
}
146138

147139
public static NormalizeIndexNode forBytearray() {
148-
return create(BYTEARRAY_OUT_OF_BOUNDS);
140+
return create(ErrorMessages.BYTEARRAY_OUT_OF_BOUNDS);
149141
}
150142
}
151143

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PY_TRUFFLE_INT_ARRAY_TO_NATIVE;
3131
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PY_TRUFFLE_LONG_ARRAY_TO_NATIVE;
3232
import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_PY_TRUFFLE_OBJECT_ARRAY_TO_NATIVE;
33-
import static com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexNode.INDEX_OUT_OF_BOUNDS;
3433
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
3534
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GE__;
3635
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GT__;
@@ -621,31 +620,31 @@ protected Object doScalarInt(@SuppressWarnings("unused") ContainerFactory factor
621620
@Shared("getItemScalarNode") @Cached GetItemScalarNode getItemScalarNode,
622621
@Shared("normalizeIndexNode") @Cached NormalizeIndexCustomMessageNode normalizeIndexNode,
623622
@Shared("lenNode") @Cached LenNode lenNode) {
624-
return getItemScalarNode.execute(storage, normalizeIndexNode.execute(idx, lenNode.execute(storage), INDEX_OUT_OF_BOUNDS));
623+
return getItemScalarNode.execute(storage, normalizeIndexNode.execute(idx, lenNode.execute(storage), ErrorMessages.INDEX_OUT_OF_RANGE));
625624
}
626625

627626
@Specialization
628627
protected Object doScalarLong(@SuppressWarnings("unused") ContainerFactory factoryMethod, SequenceStorage storage, long idx,
629628
@Shared("getItemScalarNode") @Cached GetItemScalarNode getItemScalarNode,
630629
@Shared("normalizeIndexNode") @Cached NormalizeIndexCustomMessageNode normalizeIndexNode,
631630
@Shared("lenNode") @Cached LenNode lenNode) {
632-
return getItemScalarNode.execute(storage, normalizeIndexNode.execute(idx, lenNode.execute(storage), INDEX_OUT_OF_BOUNDS));
631+
return getItemScalarNode.execute(storage, normalizeIndexNode.execute(idx, lenNode.execute(storage), ErrorMessages.INDEX_OUT_OF_RANGE));
633632
}
634633

635634
@Specialization
636635
protected Object doScalarPInt(@SuppressWarnings("unused") ContainerFactory factoryMethod, SequenceStorage storage, PInt idx,
637636
@Shared("getItemScalarNode") @Cached GetItemScalarNode getItemScalarNode,
638637
@Shared("normalizeIndexNode") @Cached NormalizeIndexCustomMessageNode normalizeIndexNode,
639638
@Shared("lenNode") @Cached LenNode lenNode) {
640-
return getItemScalarNode.execute(storage, normalizeIndexNode.execute(idx, lenNode.execute(storage), INDEX_OUT_OF_BOUNDS));
639+
return getItemScalarNode.execute(storage, normalizeIndexNode.execute(idx, lenNode.execute(storage), ErrorMessages.INDEX_OUT_OF_RANGE));
641640
}
642641

643642
@Specialization(guards = "!isPSlice(idx)")
644643
protected Object doScalarGeneric(@SuppressWarnings("unused") ContainerFactory factoryMethod, SequenceStorage storage, Object idx,
645644
@Shared("getItemScalarNode") @Cached GetItemScalarNode getItemScalarNode,
646645
@Shared("normalizeIndexNode") @Cached NormalizeIndexCustomMessageNode normalizeIndexNode,
647646
@Shared("lenNode") @Cached LenNode lenNode) {
648-
return getItemScalarNode.execute(storage, normalizeIndexNode.execute(idx, lenNode.execute(storage), INDEX_OUT_OF_BOUNDS));
647+
return getItemScalarNode.execute(storage, normalizeIndexNode.execute(idx, lenNode.execute(storage), ErrorMessages.INDEX_OUT_OF_RANGE));
649648
}
650649

651650
@Specialization
@@ -912,7 +911,7 @@ protected static SequenceStorage doScalarInt(GenNodeSupplier generalizationNodeP
912911
@Shared("doGenNode") @Cached DoGeneralizationNode doGenNode,
913912
@Shared("normalizeNode") @Cached NormalizeIndexCustomMessageNode normalizeNode,
914913
@Shared("lenNode") @Cached LenNode lenNode) {
915-
int normalized = normalizeNode.execute(idx, lenNode.execute(storage), INDEX_OUT_OF_BOUNDS);
914+
int normalized = normalizeNode.execute(idx, lenNode.execute(storage), ErrorMessages.INDEX_OUT_OF_RANGE);
916915
try {
917916
setItemScalarNode.execute(storage, normalized, value);
918917
return storage;
@@ -936,7 +935,7 @@ protected static SequenceStorage doScalarLong(GenNodeSupplier generalizationNode
936935
@Shared("doGenNode") @Cached DoGeneralizationNode doGenNode,
937936
@Shared("normalizeNode") @Cached NormalizeIndexCustomMessageNode normalizeNode,
938937
@Shared("lenNode") @Cached LenNode lenNode) {
939-
int normalized = normalizeNode.execute(idx, lenNode.execute(storage), INDEX_OUT_OF_BOUNDS);
938+
int normalized = normalizeNode.execute(idx, lenNode.execute(storage), ErrorMessages.INDEX_OUT_OF_RANGE);
940939
try {
941940
setItemScalarNode.execute(storage, normalized, value);
942941
return storage;
@@ -955,7 +954,7 @@ protected static SequenceStorage doScalarPInt(GenNodeSupplier generalizationNode
955954
@Shared("doGenNode") @Cached DoGeneralizationNode doGenNode,
956955
@Shared("normalizeNode") @Cached NormalizeIndexCustomMessageNode normalizeNode,
957956
@Shared("lenNode") @Cached LenNode lenNode) {
958-
int normalized = normalizeNode.execute(idx, lenNode.execute(storage), INDEX_OUT_OF_BOUNDS);
957+
int normalized = normalizeNode.execute(idx, lenNode.execute(storage), ErrorMessages.INDEX_OUT_OF_RANGE);
959958
try {
960959
setItemScalarNode.execute(storage, normalized, value);
961960
return storage;
@@ -974,7 +973,7 @@ protected static SequenceStorage doScalarGeneric(GenNodeSupplier generalizationN
974973
@Shared("doGenNode") @Cached DoGeneralizationNode doGenNode,
975974
@Shared("normalizeNode") @Cached NormalizeIndexCustomMessageNode normalizeNode,
976975
@Shared("lenNode") @Cached LenNode lenNode) {
977-
int normalized = normalizeNode.execute(idx, lenNode.execute(storage), INDEX_OUT_OF_BOUNDS);
976+
int normalized = normalizeNode.execute(idx, lenNode.execute(storage), ErrorMessages.INDEX_OUT_OF_RANGE);
978977
try {
979978
setItemScalarNode.execute(storage, normalized, value);
980979
return storage;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public abstract class ErrorMessages {
6060
public static final String ARG_NOT_IN_RANGE = "%s arg not in range(%s)";
6161
public static final String ARG_SHOULD_NOT_EXCEED = "%s argument should not exceed %l";
6262
public static final String ARGS_MUST_HAVE_SAME_LENGTH = "%s arguments must have same length";
63+
public static final String ARRAY_ASSIGN_OUT_OF_BOUNDS = "array assignment index out of range";
64+
public static final String ARRAY_OUT_OF_BOUNDS = "array index out of range";
6365
public static final String ARRAY_SIZE_TOO_LARGE = "array size too large";
6466
public static final String ATTEMP_TO_RELEASE_RECURSIVE_LOCK = "attempt to release recursive lock not owned by thread";
6567
public static final String ATTEMPT_TO_ASSIGN_SEQ_OF_SIZE_TO_SLICE_OF_SIZE = "attempt to assign sequence of size %d to extended slice of size %d";
@@ -83,6 +85,7 @@ public abstract class ErrorMessages {
8385
public static final String BOOL_SHOULD_RETURN_BOOL = "__bool__ should return bool, returned %p";
8486
public static final String BUFFER_INDICES_MUST_BE_INTS = "buffer indices must be integers, not %p";
8587
public static final String BYTE_STR_IS_TOO_LARGE = "byte string is too large";
88+
public static final String BYTEARRAY_OUT_OF_BOUNDS = "bytearray index out of range";
8689
public static final String BYTEORDER_MUST_BE_LITTLE_OR_BIG = "byteorder must be either 'little' or 'big'";
8790
public static final String BYTES_OBJ_REQUIRED = "a bytes-like object is required, not '%p'";
8891
public static final String BYTESLIKE_OBJ_REQUIRED = "a bytes-like object is required, not '%p'";
@@ -232,6 +235,7 @@ public abstract class ErrorMessages {
232235
public static final String INCOMPLETE_FORMAT = "incomplete format";
233236
public static final String INDEX_NOT_INT = "%s: index not int";
234237
public static final String INDEX_OUT_OF_BOUNDS = "index out of bounds";
238+
public static final String INDEX_OUT_OF_RANGE = "index out of range";
235239
public static final String INDEX_RETURNED_NON_INT = "__index__ returned non-int (type %p)";
236240
public static final String INSTANCE_EX_MAY_NOT_HAVE_SEP_VALUE = "instance exception may not have a separate value";
237241
public static final String INSTANCE_HAS_NO_ATTR_S = "%s instance has no attribute '%s'";
@@ -371,7 +375,8 @@ public abstract class ErrorMessages {
371375
public static final String PROVIDED_OBJ_NOT_ARRAY = "provided object is not an array";
372376
public static final String PYTHON_INT_TOO_LARGE_TO_CONV_TO = "Python int too large to convert to %s";
373377
public static final String PYTHON_INT_TOO_LARGE_TO_CONV_TO_C_TYPE = "Python int too large to convert to %s-byte C type";
374-
public static final String RAGE_DOES_NOT_SUPPORT = "range does not support %s, %s, %s";
378+
public static final String RANGE_OUT_OF_BOUNDS = "range index out of range";
379+
public static final String RANGE_DOES_NOT_SUPPORT = "range does not support %s, %s, %s";
375380
public static final String RAW_FORMAT_NOT_SUPPORTED = "RAW format unsupported";
376381
public static final String READ_BYTE_OUT_OF_RANGE = "read byte out of range";
377382
public static final String READ_WRITE_BYTELIKE_OBJ = "read-write bytes-like object";
@@ -434,6 +439,8 @@ public abstract class ErrorMessages {
434439
public static final String TOO_MANY_VALUES_TO_UNPACK = "too many values to unpack (expected %d)";
435440
public static final String TRAILING_S_IN_STR = "Trailing %s in string";
436441
public static final String TRANS_TABLE_MUST_BE_256 = "translation table must be 256 characters long";
442+
public static final String TUPLE_ASSIGN_OUT_OF_BOUNDS = "tuple assignment index out of range";
443+
public static final String TUPLE_OUT_OF_BOUNDS = "tuple index out of range";
437444
public static final String TUPLE_OR_STRUCT_TIME_ARG_REQUIRED = "Tuple or struct_time argument required";
438445
public static final String TWO_STARRED_EXPRESSION_IN_ASSIGMENT = "two starred expressions in assignment";
439446
public static final String TYPE_DOES_NOT_PROVIDE_BASES = "type does not provide bases";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/PMutableSequence.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
4646
import com.oracle.graal.python.builtins.objects.ints.PInt;
4747
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
48+
import com.oracle.graal.python.nodes.ErrorMessages;
4849
import com.oracle.graal.python.runtime.exception.PException;
4950
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
5051
import com.oracle.graal.python.util.OverflowException;
@@ -77,7 +78,7 @@ public boolean isArrayElementModifiable(long index,
7778
@Exclusive @Cached IndexNodes.NormalizeIndexCustomMessageNode normalize) {
7879
final int len = lenNode.execute(getSequenceStorageNode.execute(this));
7980
try {
80-
normalize.execute(index, len, IndexNodes.NormalizeIndexNode.INDEX_OUT_OF_BOUNDS);
81+
normalize.execute(index, len, ErrorMessages.INDEX_OUT_OF_RANGE);
8182
} catch (PException e) {
8283
return false;
8384
}
@@ -99,7 +100,7 @@ public boolean isArrayElementRemovable(long index,
99100
@Exclusive @Cached IndexNodes.NormalizeIndexCustomMessageNode normalize) {
100101
final int len = lenNode.execute(getSequenceStorageNode.execute(this));
101102
try {
102-
normalize.execute(index, len, IndexNodes.NormalizeIndexNode.INDEX_OUT_OF_BOUNDS);
103+
normalize.execute(index, len, ErrorMessages.INDEX_OUT_OF_RANGE);
103104
} catch (PException e) {
104105
return false;
105106
}

0 commit comments

Comments
 (0)