Skip to content

Commit 727729c

Browse files
committed
[GR-31295] IndexNodes do not support long length.
1 parent 99c6832 commit 727729c

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ public int execute(boolean index, int length) {
8888
return subNode.execute(index, length, errorMessage);
8989
}
9090

91+
public long executeLong(long index, long length) {
92+
return subNode.executeLong(index, length, errorMessage);
93+
}
94+
9195
protected final String getErrorMessage() {
9296
return errorMessage;
9397
}
@@ -154,6 +158,8 @@ public abstract static class NormalizeIndexCustomMessageNode extends Node {
154158

155159
public abstract int execute(int index, int length, String errorMessage);
156160

161+
public abstract long executeLong(long index, long length, String errorMessage);
162+
157163
public static NormalizeIndexCustomMessageNode create() {
158164
return NormalizeIndexWithBoundsCheckNodeGen.create();
159165
}
@@ -234,6 +240,18 @@ int doPIntOvf(PInt index, int length, String errorMessage,
234240
}
235241
}
236242

243+
@Specialization
244+
static long doLongLong(long lIndex, long length, String errorMessage,
245+
@Shared("negativeIndexProfile") @Cached ConditionProfile negativeIndexProfile,
246+
@Shared("boundsCheckNode") @Cached BoundsCheckNode boundsCheckNode) {
247+
long normalizedIndex = lIndex;
248+
if (negativeIndexProfile.profile(normalizedIndex < 0)) {
249+
normalizedIndex += length;
250+
}
251+
boundsCheckNode.execute(errorMessage, normalizedIndex, length);
252+
return normalizedIndex;
253+
}
254+
237255
}
238256

239257
@GenerateUncached
@@ -289,17 +307,38 @@ static int doPIntOvf(PInt index, int length, String errorMessage,
289307
throw raiseNode.raiseNumberTooLarge(PythonBuiltinClassType.IndexError, index);
290308
}
291309
}
310+
311+
@Specialization
312+
static long doLongLong(long index, long length, @SuppressWarnings("unused") String errorMessage,
313+
@Shared("negativeIndexProfile") @Cached ConditionProfile negativeIndexProfile) {
314+
long idx = index;
315+
if (negativeIndexProfile.profile(idx < 0)) {
316+
idx += length;
317+
}
318+
return idx;
319+
}
292320
}
293321

294322
@GenerateUncached
295323
public abstract static class BoundsCheckNode extends Node {
296324

297325
public abstract void execute(String errorMessage, int idx, int length);
298326

327+
public abstract void execute(String errorMessage, long idx, long length);
328+
299329
@Specialization
300330
static void doBoundsCheck(String errorMessage, int idx, int length,
301331
@Cached ConditionProfile outOfBoundsProfile,
302-
@Cached PRaiseNode raiseNode) {
332+
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
333+
if (outOfBoundsProfile.profile(idx < 0 || idx >= length)) {
334+
throw raiseNode.raise(PythonBuiltinClassType.IndexError, errorMessage);
335+
}
336+
}
337+
338+
@Specialization
339+
static void doBoundsCheck(String errorMessage, long idx, long length,
340+
@Cached ConditionProfile outOfBoundsProfile,
341+
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
303342
if (outOfBoundsProfile.profile(idx < 0 || idx >= length)) {
304343
throw raiseNode.raise(PythonBuiltinClassType.IndexError, errorMessage);
305344
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/foreign/AccessForeignItemNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ Object doArraySlice(Object object, PSlice idxSlice,
151151
Object doArrayIndex(Object object, Object key,
152152
@Cached NormalizeIndexNode normalize,
153153
@Shared("lib") @CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") InteropLibrary lib) {
154-
if (lib.isNumber(key) && lib.fitsInInt(key)) {
154+
if (lib.isNumber(key) && lib.fitsInLong(key)) {
155155
try {
156-
return readForeignIndex(object, normalize.execute(lib.asInt(key), (int) lib.getArraySize(object)), lib);
156+
return readForeignIndex(object, normalize.executeLong(lib.asLong(key), lib.getArraySize(object)), lib);
157157
} catch (UnsupportedMessageException e) {
158158
throw CompilerDirectives.shouldNotReachHere(e);
159159
}

0 commit comments

Comments
 (0)