Skip to content

Commit 88d4936

Browse files
committed
[GR-21744] Fix multiple interop compliance errors
1 parent afa50aa commit 88d4936

4 files changed

Lines changed: 55 additions & 30 deletions

File tree

graalpython/com.oracle.graal.python.tck/src/com/oracle/graal/python/tck/PythonProvider.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,16 @@ public void accept(SnippetRun snippetRun) throws PolyglotException {
419419
assert snippetRun.getException() == null;
420420
TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
421421
assert STRING.isAssignable(resultType);
422+
} else if ((par0.isNumber() || par0.isBoolean()) && (par1.isNumber() || par1.isBoolean())) {
423+
// number * number has greater precendence than array * number
424+
assert snippetRun.getException() == null;
425+
TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
426+
assert NUMBER.isAssignable(resultType) : resultType.toString();
422427
} else if (isArrayMul(par0, par1) || isArrayMul(par1, par0)) {
423428
// array * number => array
424429
assert snippetRun.getException() == null;
425430
TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
426431
assert array(ANY).isAssignable(resultType);
427-
} else if ((par0.isNumber() || par0.isBoolean()) && (par1.isNumber() || par1.isBoolean())) {
428-
assert snippetRun.getException() == null;
429-
TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
430-
assert NUMBER.isAssignable(resultType);
431432
} else {
432433
assert snippetRun.getException() != null;
433434
TypeDescriptor argType = union(STRING, BOOLEAN, NUMBER, array(ANY));
@@ -472,7 +473,7 @@ public void accept(SnippetRun snippetRun) throws PolyglotException {
472473
assert snippetRun.getException() != null;
473474
return;
474475
}
475-
if ((idx >= 0 && len > idx) || (len - idx >= 0 && len > len - idx)) {
476+
if ((idx >= 0 && len > idx) || (idx < 0 && idx + len >= 0 && len > idx + len)) {
476477
assert snippetRun.getException() == null : snippetRun.getException().toString();
477478
} else {
478479
assert snippetRun.getException() != null;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -273,22 +273,16 @@ public boolean hasArrayElements(
273273

274274
@ExportMessage
275275
public Object readArrayElement(long key,
276-
@CachedLibrary("this") PythonObjectLibrary dataModelLibrary,
276+
@CachedLibrary("this") InteropLibrary interopLib,
277277
@Shared("getItemNode") @Cached PInteropSubscriptNode getItemNode,
278278
@Exclusive @Cached GilNode gil) throws UnsupportedMessageException, InvalidArrayIndexException {
279279
boolean mustRelease = gil.acquire();
280280
try {
281-
if (dataModelLibrary.isSequence(this)) {
281+
if (interopLib.hasArrayElements(this)) {
282282
try {
283283
return getItemNode.execute(this, key);
284284
} catch (PException e) {
285-
if (isAbstractMapping(dataModelLibrary)) {
286-
throw UnsupportedMessageException.create();
287-
} else {
288-
// TODO(fa) refine exception handling
289-
// it's a sequence, so we assume the index is wrong
290-
throw InvalidArrayIndexException.create(key);
291-
}
285+
throw InvalidArrayIndexException.create(key);
292286
}
293287
}
294288
throw UnsupportedMessageException.create();
@@ -299,38 +293,35 @@ public Object readArrayElement(long key,
299293

300294
@ExportMessage
301295
public void writeArrayElement(long key, Object value,
302-
@CachedLibrary("this") PythonObjectLibrary dataModelLibrary,
296+
@CachedLibrary("this") InteropLibrary interopLib,
303297
@Cached PInteropSubscriptAssignNode setItemNode,
304298
@Exclusive @Cached GilNode gil) throws UnsupportedMessageException, InvalidArrayIndexException {
305299
boolean mustRelease = gil.acquire();
306300
try {
307-
if (dataModelLibrary.isSequence(this)) {
301+
if (interopLib.hasArrayElements(this)) {
308302
try {
309303
setItemNode.execute(this, key, value);
310304
} catch (PException e) {
311-
// TODO(fa) refine exception handling
312-
// it's a sequence, so we assume the index is wrong
313305
throw InvalidArrayIndexException.create(key);
314306
}
315307
}
308+
throw UnsupportedMessageException.create();
316309
} finally {
317310
gil.release(mustRelease);
318311
}
319312
}
320313

321314
@ExportMessage
322315
public void removeArrayElement(long key,
323-
@CachedLibrary("this") PythonObjectLibrary dataModelLibrary,
316+
@CachedLibrary("this") InteropLibrary interopLib,
324317
@Exclusive @Cached PInteropDeleteItemNode deleteItemNode,
325318
@Exclusive @Cached GilNode gil) throws UnsupportedMessageException, InvalidArrayIndexException {
326319
boolean mustRelease = gil.acquire();
327320
try {
328-
if (dataModelLibrary.isSequence(this)) {
321+
if (interopLib.hasArrayElements(this)) {
329322
try {
330323
deleteItemNode.execute(this, key);
331324
} catch (PException e) {
332-
// TODO(fa) refine exception handling
333-
// it's a sequence, so we assume the index is wrong
334325
throw InvalidArrayIndexException.create(key);
335326
}
336327
} else {
@@ -343,12 +334,14 @@ public void removeArrayElement(long key,
343334

344335
@ExportMessage
345336
public long getArraySize(
337+
@CachedLibrary("this") InteropLibrary interopLib,
346338
@Shared("sizeNode") @Cached PyObjectSizeNode sizeNode,
347339
@Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
348340
boolean mustRelease = gil.acquire();
341+
if (!interopLib.hasArrayElements(this)) {
342+
throw UnsupportedMessageException.create();
343+
}
349344
try {
350-
// since a call to this method must be preceded by a call to 'hasArrayElements', we just
351-
// assume that a length exists
352345
long len = sizeNode.execute(null, this);
353346
if (len >= 0) {
354347
return len;
@@ -362,10 +355,14 @@ public long getArraySize(
362355

363356
@ExportMessage
364357
public boolean isArrayElementReadable(@SuppressWarnings("unused") long idx,
358+
@CachedLibrary("this") InteropLibrary interopLib,
365359
@Shared("sizeNode") @Cached PyObjectSizeNode sizeNode,
366360
@Shared("getItemNode") @Cached PInteropSubscriptNode getItemNode,
367361
@Exclusive @Cached GilNode gil) {
368362
boolean mustRelease = gil.acquire();
363+
if (!interopLib.hasArrayElements(this)) {
364+
return false;
365+
}
369366
try {
370367
return isInBounds(sizeNode.execute(null, this), getItemNode, idx);
371368
} finally {
@@ -375,10 +372,14 @@ public boolean isArrayElementReadable(@SuppressWarnings("unused") long idx,
375372

376373
@ExportMessage
377374
public boolean isArrayElementModifiable(@SuppressWarnings("unused") long idx,
375+
@CachedLibrary("this") InteropLibrary interopLib,
378376
@Shared("sizeNode") @Cached PyObjectSizeNode sizeNode,
379377
@Shared("getItemNode") @Cached PInteropSubscriptNode getItemNode,
380378
@Exclusive @Cached GilNode gil) {
381379
boolean mustRelease = gil.acquire();
380+
if (!interopLib.hasArrayElements(this)) {
381+
return false;
382+
}
382383
try {
383384
return !(this instanceof PTuple) && !(this instanceof PBytes) && isInBounds(sizeNode.execute(null, this), getItemNode, idx);
384385
} finally {
@@ -388,10 +389,14 @@ public boolean isArrayElementModifiable(@SuppressWarnings("unused") long idx,
388389

389390
@ExportMessage
390391
public boolean isArrayElementInsertable(@SuppressWarnings("unused") long idx,
392+
@CachedLibrary("this") InteropLibrary interopLib,
391393
@Shared("sizeNode") @Cached PyObjectSizeNode sizeNode,
392394
@Shared("getItemNode") @Cached PInteropSubscriptNode getItemNode,
393395
@Exclusive @Cached GilNode gil) {
394396
boolean mustRelease = gil.acquire();
397+
if (!interopLib.hasArrayElements(this)) {
398+
return false;
399+
}
395400
try {
396401
return !(this instanceof PTuple) && !(this instanceof PBytes) && !isInBounds(sizeNode.execute(null, this), getItemNode, idx);
397402
} finally {
@@ -401,10 +406,14 @@ public boolean isArrayElementInsertable(@SuppressWarnings("unused") long idx,
401406

402407
@ExportMessage
403408
public boolean isArrayElementRemovable(@SuppressWarnings("unused") long idx,
409+
@CachedLibrary("this") InteropLibrary interopLib,
404410
@Shared("sizeNode") @Cached PyObjectSizeNode sizeNode,
405411
@Shared("getItemNode") @Cached PInteropSubscriptNode getItemNode,
406412
@Exclusive @Cached GilNode gil) {
407413
boolean mustRelease = gil.acquire();
414+
if (!interopLib.hasArrayElements(this)) {
415+
return false;
416+
}
408417
try {
409418
return !(this instanceof PTuple) && !(this instanceof PBytes) && isInBounds(sizeNode.execute(null, this), getItemNode, idx);
410419
} finally {
@@ -606,9 +615,13 @@ public boolean isInstantiable(
606615

607616
@ExportMessage
608617
public Object instantiate(Object[] arguments,
618+
@CachedLibrary("this") InteropLibrary interopLib,
609619
@Exclusive @Cached PExecuteNode executeNode,
610620
@Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
611621
boolean mustRelease = gil.acquire();
622+
if (!interopLib.isInstantiable(this)) {
623+
throw UnsupportedMessageException.create();
624+
}
612625
try {
613626
return executeNode.execute(this, arguments);
614627
} finally {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/PBaseException.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,18 @@ public String toString() {
229229
if (messageArgs != null && messageArgs.length > 0) {
230230
sb.append("(fmt=\"").append(messageFormat).append("\", args = (");
231231
for (Object arg : messageArgs) {
232-
sb.append(arg).append(", ");
232+
if (arg instanceof PythonObject) {
233+
sb.append(arg);
234+
} else {
235+
String fqn = arg.getClass().getName();
236+
if (fqn.startsWith("java.lang.")) {
237+
sb.append(arg);
238+
} else {
239+
// we do not risk printing arbitrary objects
240+
sb.append("a ").append(fqn);
241+
}
242+
}
243+
sb.append(", ");
233244
}
234245
sb.append(") )");
235246
} else if (hasMessageFormat) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ Object doComparisonBool(VirtualFrame frame, Object left, Object right,
256256

257257
}
258258

259-
@Specialization(guards = {"lib.fitsInLong(left)"})
259+
@Specialization(guards = {"!lib.isBoolean(left)", "lib.fitsInLong(left)"})
260260
Object doComparisonLong(VirtualFrame frame, Object left, Object right,
261261
@CachedLibrary(limit = "3") InteropLibrary lib,
262262
@Cached GilNode gil) {
@@ -277,7 +277,7 @@ Object doComparisonLong(VirtualFrame frame, Object left, Object right,
277277
}
278278
}
279279

280-
@Specialization(guards = {"!lib.fitsInLong(left)", "lib.fitsInDouble(left)"})
280+
@Specialization(guards = {"!lib.isBoolean(left)", "!lib.fitsInLong(left)", "lib.fitsInDouble(left)"})
281281
Object doComparisonDouble(VirtualFrame frame, Object left, Object right,
282282
@CachedLibrary(limit = "3") InteropLibrary lib,
283283
@Cached GilNode gil) {
@@ -298,7 +298,7 @@ Object doComparisonDouble(VirtualFrame frame, Object left, Object right,
298298
}
299299
}
300300

301-
@Specialization(guards = {"lib.isString(left)"})
301+
@Specialization(guards = {"!lib.isBoolean(left)", "!lib.fitsInLong(left)", "!lib.fitsInDouble(left)", "lib.isString(left)"})
302302
Object doComparisonString(VirtualFrame frame, Object left, Object right,
303303
@CachedLibrary(limit = "3") InteropLibrary lib,
304304
@Cached GilNode gil) {
@@ -376,7 +376,7 @@ abstract static class MulNode extends ForeignBinaryNode {
376376
super(BinaryArithmetic.Mul.create(), false);
377377
}
378378

379-
@Specialization(insertBefore = "doComparisonBool", guards = {"!lib.isNumber(left)", "lib.hasArrayElements(left)", "lib.fitsInLong(right)"})
379+
@Specialization(insertBefore = "doComparisonBool", guards = {"!lib.isBoolean(left)", "!lib.isNumber(left)", "lib.hasArrayElements(left)", "lib.fitsInLong(right)"})
380380
static Object doForeignArray(Object left, Object right,
381381
@Cached PRaiseNode raise,
382382
@Cached PythonObjectFactory factory,
@@ -399,7 +399,7 @@ static Object doForeignArray(Object left, Object right,
399399
}
400400
}
401401

402-
@Specialization(insertBefore = "doComparisonBool", guards = {"!lib.isNumber(left)", "lib.hasArrayElements(left)", "lib.isBoolean(right)"})
402+
@Specialization(insertBefore = "doComparisonBool", guards = {"!lib.isBoolean(left)", "!lib.isNumber(left)", "lib.hasArrayElements(left)", "lib.isBoolean(right)"})
403403
static Object doForeignArrayForeignBoolean(Object left, Object right,
404404
@Cached PRaiseNode raise,
405405
@Cached PythonObjectFactory factory,

0 commit comments

Comments
 (0)