Skip to content

Commit a81cb6b

Browse files
committed
Allow different error types when casting to index-sized int.
1 parent f01f674 commit a81cb6b

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ protected boolean isInt(Object o) {
252252
protected CastToIndexNode getCastToIndexNode() {
253253
if (castToIndexNode == null) {
254254
CompilerDirectives.transferToInterpreterAndInvalidate();
255-
castToIndexNode = insert(CastToIndexNode.create());
255+
castToIndexNode = insert(CastToIndexNode.createOverflow());
256256
}
257257
return castToIndexNode;
258258
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/CastToIndexNode.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141
package com.oracle.graal.python.nodes.util;
4242

4343
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INDEX__;
44+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.IndexError;
45+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
4446
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
4547

4648
import com.oracle.graal.python.builtins.objects.ints.PInt;
4749
import com.oracle.graal.python.nodes.PBaseNode;
4850
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
49-
import com.oracle.graal.python.runtime.exception.PException;
51+
import com.oracle.graal.python.runtime.exception.PythonErrorType;
5052
import com.oracle.truffle.api.CompilerDirectives;
5153
import com.oracle.truffle.api.dsl.Fallback;
5254
import com.oracle.truffle.api.dsl.Specialization;
@@ -58,8 +60,16 @@
5860
*/
5961
public abstract class CastToIndexNode extends PBaseNode {
6062

63+
private static final String ERROR_MESSAGE = "cannot fit 'int' into an index-sized integer";
64+
6165
@Child private LookupAndCallUnaryNode callIndexNode;
66+
6267
private final BranchProfile errorProfile = BranchProfile.create();
68+
private final PythonErrorType errorType;
69+
70+
public CastToIndexNode(PythonErrorType errorType) {
71+
this.errorType = errorType;
72+
}
6373

6474
public abstract int execute(Object x);
6575

@@ -89,7 +99,7 @@ int doLongOvf(long x) {
8999
try {
90100
return PInt.intValueExact(x);
91101
} catch (ArithmeticException e) {
92-
throw raiseIndexError();
102+
throw raise(errorType, ERROR_MESSAGE);
93103
}
94104
}
95105

@@ -103,7 +113,7 @@ int doPIntOvf(PInt x) {
103113
try {
104114
return x.intValueExact();
105115
} catch (ArithmeticException e) {
106-
throw raiseIndexError();
116+
throw raise(errorType, ERROR_MESSAGE);
107117
}
108118
}
109119

@@ -122,6 +132,10 @@ int doGeneric(Object x) {
122132
}
123133

124134
public static CastToIndexNode create() {
125-
return CastToIndexNodeGen.create();
135+
return CastToIndexNodeGen.create(IndexError);
136+
}
137+
138+
public static CastToIndexNode createOverflow() {
139+
return CastToIndexNodeGen.create(OverflowError);
126140
}
127141
}

0 commit comments

Comments
 (0)