Skip to content

Commit 5004257

Browse files
committed
update and fix SliceLiteralNode
1 parent 4b49d7d commit 5004257

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_slice.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,19 @@ def test_range_step2():
178178
assert t[100:-100:-1] == range(13, 3, -2)
179179
assert t[-100:100:-1] == range(3, 13, -2)
180180
assert t[-100:100:2] == range(5, 15, 4)
181+
182+
183+
def test_correct_error():
184+
class X():
185+
def __index__(self):
186+
return "42"
187+
188+
try:
189+
[1][:X()]
190+
except TypeError as e:
191+
assert "__index__ returned non-int" in str(e)
192+
193+
try:
194+
[1][:"42"]
195+
except TypeError as e:
196+
assert "slice indices must be integers" in str(e)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/subscript/SliceLiteralNode.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@
4040
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
4141
import com.oracle.graal.python.nodes.subscript.SliceLiteralNodeGen.CastToSliceComponentNodeGen;
4242
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
43-
import com.oracle.graal.python.nodes.util.CastToIndexNode;
43+
import com.oracle.graal.python.runtime.PythonOptions;
4444
import com.oracle.graal.python.runtime.exception.PException;
4545
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4646
import com.oracle.truffle.api.CompilerDirectives;
4747
import com.oracle.truffle.api.dsl.Cached;
4848
import com.oracle.truffle.api.dsl.Fallback;
49+
import com.oracle.truffle.api.dsl.ImportStatic;
4950
import com.oracle.truffle.api.dsl.NodeChild;
5051
import com.oracle.truffle.api.dsl.Specialization;
5152
import com.oracle.truffle.api.dsl.TypeSystemReference;
@@ -118,6 +119,7 @@ public static SliceLiteralNode create() {
118119
return SliceLiteralNodeGen.create(null, null, null);
119120
}
120121

122+
@ImportStatic(PythonOptions.class)
121123
abstract static class CastToSliceComponentNode extends PNodeWithContext {
122124

123125
@Child private PRaiseNode raiseNode;
@@ -174,28 +176,19 @@ int doPInt(PInt i) {
174176

175177
@Specialization(replaces = {"doBoolean", "doInt", "doLong", "doPInt"}, limit = "getCallSiteInlineCacheMaxDepth()")
176178
int doGeneric(VirtualFrame frame, Object i,
179+
@Cached PRaiseNode raise,
177180
@CachedLibrary("i") PythonObjectLibrary lib,
178181
@Cached IsBuiltinClassProfile errorProfile) {
179-
try {
180-
return lib.asIndexWithState(i, PArguments.getThreadState(frame));
181-
} catch (PException e) {
182-
e.expect(PythonBuiltinClassType.OverflowError, errorProfile);
183-
return overflowValue;
184-
}
185-
}
186-
187-
protected CastToIndexNode createCastToIndex() {
188-
return CastToIndexNode.create(PythonBuiltinClassType.OverflowError, val -> {
189-
throw getRaiseNode().raise(PythonBuiltinClassType.TypeError, "slice indices must be integers or None or have an __index__ method");
190-
});
191-
}
192-
193-
private PRaiseNode getRaiseNode() {
194-
if (raiseNode == null) {
195-
CompilerDirectives.transferToInterpreterAndInvalidate();
196-
raiseNode = insert(PRaiseNode.create());
182+
if (lib.canBeIndex(i)) {
183+
try {
184+
return lib.asIndexWithState(i, PArguments.getThreadState(frame));
185+
} catch (PException e) {
186+
e.expect(PythonBuiltinClassType.OverflowError, errorProfile);
187+
return overflowValue;
188+
}
189+
} else {
190+
throw raise.raise(PythonBuiltinClassType.TypeError, "slice indices must be integers or None or have an __index__ method");
197191
}
198-
return raiseNode;
199192
}
200193

201194
public static CastToSliceComponentNode create(int defaultValue, int overflowValue) {

0 commit comments

Comments
 (0)