Skip to content

Commit 601e9e2

Browse files
committed
[GR-20145] Add asSize message to object library
PullRequest: graalpython/762
2 parents 8a48496 + 0409206 commit 601e9e2

31 files changed

+486
-633
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates.
1+
# Copyright (c) 2018, 2019, Oracle and/or its affiliates.
22
# Copyright (c) 2013, Regents of the University of California
33
#
44
# All rights reserved.
@@ -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/builtins/modules/BuiltinConstructors.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import com.oracle.graal.python.builtins.objects.floats.FloatBuiltinsFactory;
109109
import com.oracle.graal.python.builtins.objects.floats.PFloat;
110110
import com.oracle.graal.python.builtins.objects.frame.PFrame;
111+
import com.oracle.graal.python.builtins.objects.function.PArguments;
111112
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
112113
import com.oracle.graal.python.builtins.objects.function.PFunction;
113114
import com.oracle.graal.python.builtins.objects.function.PKeyword;
@@ -169,7 +170,7 @@
169170
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
170171
import com.oracle.graal.python.nodes.util.CastToByteNode;
171172
import com.oracle.graal.python.nodes.util.CastToDoubleNode;
172-
import com.oracle.graal.python.nodes.util.CastToIndexNode;
173+
import com.oracle.graal.python.nodes.util.CastToJavaIntNode;
173174
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
174175
import com.oracle.graal.python.nodes.util.CastToJavaStringNodeGen;
175176
import com.oracle.graal.python.nodes.util.CoerceToStringNode;
@@ -222,8 +223,6 @@ public void initialize(PythonCore core) {
222223

223224
@TypeSystemReference(PythonArithmeticTypes.class)
224225
protected abstract static class CreateByteOrByteArrayNode extends PythonBuiltinNode {
225-
@Child private CastToIndexNode castToIndexNode;
226-
227226
private final IsBuiltinClassProfile isClassProfile = IsBuiltinClassProfile.create();
228227

229228
@SuppressWarnings("unused")
@@ -239,7 +238,7 @@ public Object bytearray(LazyPythonClass cls, @SuppressWarnings("unused") PNone s
239238
@Specialization(guards = {"lib.canBeIndex(capObj)", "isNoValue(encoding)", "isNoValue(errors)"})
240239
public Object bytearray(VirtualFrame frame, LazyPythonClass cls, Object capObj, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
241240
@SuppressWarnings("unused") @CachedLibrary(limit = "1") PythonObjectLibrary lib) {
242-
int cap = getCastToIndexNode().execute(frame, capObj);
241+
int cap = lib.asSizeWithState(capObj, PArguments.getThreadState(frame));
243242
return create(cls, BytesUtils.fromSize(getCore(), cap));
244243
}
245244

@@ -301,14 +300,6 @@ public Object bytearray(VirtualFrame frame, LazyPythonClass cls, Object iterable
301300
private static byte[] resize(byte[] arr, int len) {
302301
return Arrays.copyOf(arr, len);
303302
}
304-
305-
protected CastToIndexNode getCastToIndexNode() {
306-
if (castToIndexNode == null) {
307-
CompilerDirectives.transferToInterpreterAndInvalidate();
308-
castToIndexNode = insert(CastToIndexNode.createOverflow());
309-
}
310-
return castToIndexNode;
311-
}
312303
}
313304

314305
// bytes([source[, encoding[, errors]]])
@@ -1205,10 +1196,10 @@ Object parsePIntError(LazyPythonClass cls, String number, int base) {
12051196
return stringToInt(cls, number, base);
12061197
}
12071198

1208-
@Specialization(guards = "!isNoValue(base)")
1199+
@Specialization(guards = "!isNoValue(base)", limit = "getCallSiteInlineCacheMaxDepth()")
12091200
Object createIntError(VirtualFrame frame, LazyPythonClass cls, String number, Object base,
1210-
@Cached CastToIndexNode castToIndexNode) {
1211-
int intBase = castToIndexNode.execute(frame, base);
1201+
@CachedLibrary("base") PythonObjectLibrary lib) {
1202+
int intBase = lib.asSizeWithState(base, PArguments.getThreadState(frame));
12121203
checkBase(intBase);
12131204
return stringToInt(cls, number, intBase);
12141205
}
@@ -2006,7 +1997,7 @@ public abstract static class TypeNode extends PythonBuiltinNode {
20061997
@Child private ReadAttributeFromObjectNode readAttrNode;
20071998
@Child private SetAttributeNode.Dynamic writeAttrNode;
20081999
@Child private GetAnyAttributeNode getAttrNode;
2009-
@Child private CastToIndexNode castToInt;
2000+
@Child private CastToJavaIntNode castToInt;
20102001
@Child private CastToListNode castToList;
20112002
@Child private CastToJavaStringNode castToStringNode;
20122003
@Child private SequenceStorageNodes.LenNode slotLenNode;
@@ -2345,9 +2336,9 @@ private boolean addDictIfNative(VirtualFrame frame, PythonManagedClass pythonCla
23452336
for (Object cls : getMro(pythonClass)) {
23462337
if (PGuards.isNativeClass(cls)) {
23472338
// Use GetAnyAttributeNode since these are get-set-descriptors
2348-
long dictoffset = ensureCastToIntNode().execute(frame, ensureGetAttributeNode().executeObject(frame, cls, __DICTOFFSET__));
2349-
long basicsize = ensureCastToIntNode().execute(frame, ensureGetAttributeNode().executeObject(frame, cls, __BASICSIZE__));
2350-
long itemsize = ensureCastToIntNode().execute(frame, ensureGetAttributeNode().executeObject(frame, cls, __ITEMSIZE__));
2339+
long dictoffset = ensureCastToIntNode().execute(ensureGetAttributeNode().executeObject(frame, cls, __DICTOFFSET__));
2340+
long basicsize = ensureCastToIntNode().execute(ensureGetAttributeNode().executeObject(frame, cls, __BASICSIZE__));
2341+
long itemsize = ensureCastToIntNode().execute(ensureGetAttributeNode().executeObject(frame, cls, __ITEMSIZE__));
23512342
if (dictoffset == 0) {
23522343
addedNewDict = true;
23532344
// add_dict
@@ -2454,10 +2445,10 @@ private SetAttributeNode.Dynamic ensureWriteAttrNode() {
24542445
return writeAttrNode;
24552446
}
24562447

2457-
private CastToIndexNode ensureCastToIntNode() {
2448+
private CastToJavaIntNode ensureCastToIntNode() {
24582449
if (castToInt == null) {
24592450
CompilerDirectives.transferToInterpreterAndInvalidate();
2460-
castToInt = insert(CastToIndexNode.create());
2451+
castToInt = insert(CastToJavaIntNode.create());
24612452
}
24622453
return castToInt;
24632454
}

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5555
import com.oracle.graal.python.builtins.PythonBuiltins;
5656
import com.oracle.graal.python.builtins.objects.PNone;
57+
import com.oracle.graal.python.builtins.objects.function.PArguments;
5758
import com.oracle.graal.python.builtins.objects.ints.PInt;
5859
import com.oracle.graal.python.builtins.objects.lzma.PLZMACompressor;
5960
import com.oracle.graal.python.builtins.objects.lzma.PLZMADecompressor;
@@ -66,7 +67,6 @@
6667
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6768
import com.oracle.graal.python.nodes.subscript.GetItemNode;
6869
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
69-
import com.oracle.graal.python.nodes.util.CastToIndexNode;
7070
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
7171
import com.oracle.graal.python.runtime.PythonContext;
7272
import com.oracle.graal.python.runtime.PythonCore;
@@ -75,7 +75,6 @@
7575
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
7676
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7777
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
78-
import com.oracle.truffle.api.dsl.Cached;
7978
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
8079
import com.oracle.truffle.api.dsl.NodeFactory;
8180
import com.oracle.truffle.api.dsl.Specialization;
@@ -173,7 +172,7 @@ public void initialize(PythonCore core) {
173172
abstract static class LZMANode extends PythonBuiltinNode {
174173

175174
@Child private GetItemNode getItemNode;
176-
@Child private CastToIndexNode castToLongNode;
175+
@Child private PythonObjectLibrary castToLongNode;
177176
@Child private BuiltinFunctions.LenNode lenNode;
178177
@CompilationFinal private IsBuiltinClassProfile keyErrorProfile;
179178

@@ -281,9 +280,9 @@ private IsBuiltinClassProfile ensureKeyErrorProfile() {
281280
private int asInt(VirtualFrame frame, Object obj) {
282281
if (castToLongNode == null) {
283282
CompilerDirectives.transferToInterpreterAndInvalidate();
284-
castToLongNode = insert(CastToIndexNode.create());
283+
castToLongNode = insert(PythonObjectLibrary.getFactory().createDispatched(2));
285284
}
286-
return castToLongNode.execute(frame, obj);
285+
return castToLongNode.asSizeWithState(obj, PArguments.getThreadState(frame));
287286
}
288287

289288
private int len(VirtualFrame frame, Object obj) {
@@ -310,21 +309,18 @@ abstract static class LZMACompressorNode extends LZMANode {
310309

311310
@Specialization
312311
PLZMACompressor doCreate(VirtualFrame frame, LazyPythonClass cls, Object formatObj, Object checkObj, Object presetObj, Object filters,
313-
@Cached CastToIndexNode castFormatToIntNode,
314-
@Cached CastToIndexNode castCheckToIntNode,
315-
@Cached CastToIndexNode castToIntNode,
316-
@CachedLibrary(limit = "1") PythonObjectLibrary dataModelLibrary) {
312+
@CachedLibrary(limit = "4") PythonObjectLibrary lib) {
317313

318314
int format = FORMAT_XZ;
319315
int check = -1;
320316
int preset = LZMA2Options.PRESET_DEFAULT;
321317

322318
if (!isNoneOrNoValue(formatObj)) {
323-
format = castFormatToIntNode.execute(frame, formatObj);
319+
format = lib.asSizeWithState(formatObj, PArguments.getThreadState(frame));
324320
}
325321

326322
if (!isNoneOrNoValue(checkObj)) {
327-
check = castCheckToIntNode.execute(frame, checkObj);
323+
check = lib.asSizeWithState(checkObj, PArguments.getThreadState(frame));
328324
}
329325

330326
if (format != FORMAT_XZ && check != -1 && check != XZ.CHECK_NONE) {
@@ -335,7 +331,7 @@ PLZMACompressor doCreate(VirtualFrame frame, LazyPythonClass cls, Object formatO
335331
}
336332

337333
if (!isNoneOrNoValue(presetObj)) {
338-
preset = castToIntNode.execute(frame, presetObj);
334+
preset = lib.asSizeWithState(presetObj, PArguments.getThreadState(frame));
339335
}
340336

341337
try {
@@ -351,7 +347,7 @@ PLZMACompressor doCreate(VirtualFrame frame, LazyPythonClass cls, Object formatO
351347
LZMA2Options lzmaOptions = parseLZMAOptions(preset);
352348
xzOutputStream = createXZOutputStream(check, bos, lzmaOptions);
353349
} else {
354-
FilterOptions[] optionsChain = parseFilterChainSpec(frame, filters, dataModelLibrary);
350+
FilterOptions[] optionsChain = parseFilterChainSpec(frame, filters, lib);
355351
xzOutputStream = createXZOutputStream(check, bos, optionsChain);
356352
}
357353
return factory().createLZMACompressor(cls, xzOutputStream, bos);
@@ -362,7 +358,7 @@ PLZMACompressor doCreate(VirtualFrame frame, LazyPythonClass cls, Object formatO
362358
LZMA2Options lzmaOptions = parseLZMAOptions(preset);
363359
lzmaOutputStream = createLZMAOutputStream(bos, lzmaOptions);
364360
} else {
365-
FilterOptions[] optionsChain = parseFilterChainSpec(frame, filters, dataModelLibrary);
361+
FilterOptions[] optionsChain = parseFilterChainSpec(frame, filters, lib);
366362
if (optionsChain.length != 1 && !(optionsChain[0] instanceof LZMA2Options)) {
367363
throw raise(ValueError, "Invalid filter chain for FORMAT_ALONE - must be a single LZMA1 filter");
368364
}
@@ -375,7 +371,7 @@ PLZMACompressor doCreate(VirtualFrame frame, LazyPythonClass cls, Object formatO
375371
LZMA2Options lzmaOptions = parseLZMAOptions(preset);
376372
lzmaOutputStream = createLZMAOutputStream(bos, lzmaOptions);
377373
} else {
378-
FilterOptions[] optionsChain = parseFilterChainSpec(frame, filters, dataModelLibrary);
374+
FilterOptions[] optionsChain = parseFilterChainSpec(frame, filters, lib);
379375
if (optionsChain.length != 1 && !(optionsChain[0] instanceof LZMA2Options)) {
380376
throw raise(ValueError, "Invalid filter chain for FORMAT_ALONE - must be a single LZMA1 filter");
381377
}
@@ -423,28 +419,26 @@ private static LZMAOutputStream createLZMAOutputStream(ByteArrayOutputStream bos
423419
abstract static class LZMADecompressorNode extends LZMANode {
424420

425421
@Child private GetItemNode getItemNode;
426-
@Child private CastToIndexNode castToLongNode;
427422
@Child private BuiltinFunctions.LenNode lenNode;
428423

429424
@CompilationFinal private IsBuiltinClassProfile keyErrorProfile;
430425

431426
@Specialization
432427
PLZMADecompressor doCreate(VirtualFrame frame, LazyPythonClass cls, Object formatObj, Object memlimitObj, Object filters,
433-
@Cached CastToIndexNode castFormatToIntNode,
434-
@Cached CastToIndexNode castCheckToIntNode) {
428+
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
435429

436430
int format = FORMAT_AUTO;
437431
int memlimit = Integer.MAX_VALUE;
438432

439433
if (!isNoneOrNoValue(formatObj)) {
440-
format = castFormatToIntNode.execute(frame, formatObj);
434+
format = lib.asSizeWithState(formatObj, PArguments.getThreadState(frame));
441435
}
442436

443437
if (!isNoneOrNoValue(memlimitObj)) {
444438
if (format == FORMAT_RAW) {
445439
throw raise(ValueError, "Cannot specify memory limit with FORMAT_RAW");
446440
}
447-
memlimit = castCheckToIntNode.execute(frame, memlimitObj);
441+
memlimit = lib.asSizeWithState(memlimitObj, PArguments.getThreadState(frame));
448442
}
449443

450444
if (format == FORMAT_RAW && isNoneOrNoValue(filters)) {

0 commit comments

Comments
 (0)