Skip to content

Commit 401033f

Browse files
committed
Adjust TCK since comparison between foreign arrays is now supported
1 parent e9e4ec3 commit 401033f

File tree

1 file changed

+58
-20
lines changed

1 file changed

+58
-20
lines changed

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

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -78,6 +78,7 @@
7878
import org.graalvm.polyglot.tck.ResultVerifier;
7979
import org.graalvm.polyglot.tck.Snippet;
8080
import org.graalvm.polyglot.tck.TypeDescriptor;
81+
import org.junit.Assert;
8182

8283
public class PythonProvider implements LanguageProvider {
8384

@@ -226,28 +227,36 @@ public Collection<? extends Snippet> createValueConstructors(Context context) {
226227
public Collection<? extends Snippet> createExpressions(Context context) {
227228
List<Snippet> snippets = new ArrayList<>();
228229

230+
TypeDescriptor numeric = union(BOOLEAN, NUMBER);
231+
TypeDescriptor numericArray = array(numeric);
232+
229233
// @formatter:off
230-
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", NUMBER, new NonPrimitiveNumberParameterThrows(AddVerifier.INSTANCE), union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
231-
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, array(ANY)), AddVerifier.INSTANCE, union(BOOLEAN, NUMBER), union(STRING, array(ANY)));
232-
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, array(ANY)), AddVerifier.INSTANCE, union(STRING, array(ANY)), union(BOOLEAN, NUMBER));
234+
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", NUMBER, new NonPrimitiveNumberParameterThrows(AddVerifier.INSTANCE), numeric, numeric);
235+
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, array(ANY)), AddVerifier.INSTANCE, numeric, union(STRING, array(ANY)));
236+
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, array(ANY)), AddVerifier.INSTANCE, union(STRING, array(ANY)), numeric);
233237
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, array(ANY)), AddVerifier.INSTANCE, union(STRING, array(ANY)), union(STRING, array(ANY)));
234-
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", NUMBER, new NonPrimitiveNumberParameterThrows(MulVerifier.INSTANCE), union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
235-
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(STRING, array(ANY)), MulVerifier.INSTANCE, union(BOOLEAN, NUMBER), union(STRING, array(ANY)));
236-
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(STRING, array(ANY)), MulVerifier.INSTANCE, union(STRING, array(ANY)), union(BOOLEAN, NUMBER));
238+
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", NUMBER, new NonPrimitiveNumberParameterThrows(MulVerifier.INSTANCE), numeric, numeric);
239+
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(STRING, array(ANY)), MulVerifier.INSTANCE, numeric, union(STRING, array(ANY)));
240+
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(STRING, array(ANY)), MulVerifier.INSTANCE, union(STRING, array(ANY)), numeric);
237241
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(STRING, array(ANY)), MulVerifier.INSTANCE, union(STRING, array(ANY)), union(STRING, array(ANY)));
238242

239-
addExpressionSnippet(context, snippets, "-", "lambda x, y: x - y", NUMBER, NonPrimitiveNumberParameterThrows.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
243+
addExpressionSnippet(context, snippets, "-", "lambda x, y: x - y", NUMBER, NonPrimitiveNumberParameterThrows.INSTANCE, numeric, numeric);
240244

241-
addExpressionSnippet(context, snippets, "/", "lambda x, y: x / y", NUMBER, new NonPrimitiveNumberParameterThrows(PDivByZeroVerifier.INSTANCE), union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
245+
addExpressionSnippet(context, snippets, "/", "lambda x, y: x / y", NUMBER, new NonPrimitiveNumberParameterThrows(PDivByZeroVerifier.INSTANCE), numeric, numeric);
242246

243247
addExpressionSnippet(context, snippets, "list-from-foreign", "lambda x: list(x)", array(ANY), union(STRING, iterable(ANY), iterator(ANY), array(ANY), hash(ANY, ANY)));
244248

245249
addExpressionSnippet(context, snippets, "==", "lambda x, y: x == y", BOOLEAN, ANY, ANY);
246250
addExpressionSnippet(context, snippets, "!=", "lambda x, y: x != y", BOOLEAN, ANY, ANY);
247-
addExpressionSnippet(context, snippets, ">", "lambda x, y: x > y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
248-
addExpressionSnippet(context, snippets, ">=", "lambda x, y: x >= y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
249-
addExpressionSnippet(context, snippets, "<", "lambda x, y: x < y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
250-
addExpressionSnippet(context, snippets, "<=", "lambda x, y: x <= y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
251+
252+
addExpressionSnippet(context, snippets, ">", "lambda x, y: x > y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, numeric, numeric);
253+
addExpressionSnippet(context, snippets, ">", "lambda x, y: x > y", BOOLEAN, ExpectTypeErrorForDifferentPythonSequenceTypes.INSTANCE, numericArray, numericArray);
254+
addExpressionSnippet(context, snippets, ">=", "lambda x, y: x >= y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, numeric, numeric);
255+
addExpressionSnippet(context, snippets, ">=", "lambda x, y: x >= y", BOOLEAN, ExpectTypeErrorForDifferentPythonSequenceTypes.INSTANCE, numericArray, numericArray);
256+
addExpressionSnippet(context, snippets, "<", "lambda x, y: x < y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, numeric, numeric);
257+
addExpressionSnippet(context, snippets, "<", "lambda x, y: x < y", BOOLEAN, ExpectTypeErrorForDifferentPythonSequenceTypes.INSTANCE, numericArray, numericArray);
258+
addExpressionSnippet(context, snippets, "<=", "lambda x, y: x <= y", BOOLEAN, NonPrimitiveNumberParameterThrows.INSTANCE, numeric, numeric);
259+
addExpressionSnippet(context, snippets, "<=", "lambda x, y: x <= y", BOOLEAN, ExpectTypeErrorForDifferentPythonSequenceTypes.INSTANCE, numericArray, numericArray);
251260

252261
addExpressionSnippet(context, snippets, "isinstance", "lambda x, y: isinstance(x, y)", BOOLEAN, ANY, META_OBJECT);
253262
addExpressionSnippet(context, snippets, "issubclass", "lambda x, y: issubclass(x, y)", BOOLEAN, META_OBJECT, META_OBJECT);
@@ -360,6 +369,35 @@ private static Source createSource(String resourceName) throws IOException {
360369
private abstract static class PResultVerifier implements ResultVerifier {
361370
}
362371

372+
private static class ExpectTypeErrorForDifferentPythonSequenceTypes extends PResultVerifier {
373+
374+
public void accept(SnippetRun snippetRun) throws PolyglotException {
375+
List<? extends Value> parameters = snippetRun.getParameters();
376+
assert parameters.size() == 2;
377+
378+
Value a = parameters.get(0);
379+
Value b = parameters.get(1);
380+
381+
// If both parameter values are Python sequences, then ignore if they have different
382+
// types. E.g. ignore '(1,2) < [3,4]'.
383+
if (a.hasArrayElements() && b.hasArrayElements() && isPythonObject(a) && isPythonObject(b) && !a.getMetaObject().equals(b.getMetaObject())) {
384+
// Expect TypeError
385+
var exception = snippetRun.getException();
386+
assert exception != null;
387+
Assert.assertEquals("TypeError", exception.getGuestObject().getMetaObject().getMetaQualifiedName());
388+
} else {
389+
ResultVerifier.getDefaultResultVerifier().accept(snippetRun);
390+
}
391+
}
392+
393+
private static boolean isPythonObject(Value value) {
394+
var metaObject = value.getMetaObject();
395+
return metaObject.hasMember("__mro__");
396+
}
397+
398+
private static final ExpectTypeErrorForDifferentPythonSequenceTypes INSTANCE = new ExpectTypeErrorForDifferentPythonSequenceTypes();
399+
}
400+
363401
/**
364402
* Only accepts exact matches of types.
365403
*/
@@ -378,16 +416,16 @@ public void accept(SnippetRun snippetRun) throws PolyglotException {
378416
if (par0.getMetaObject() == par1.getMetaObject()) {
379417
assert snippetRun.getException() == null;
380418
TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
381-
assert array(ANY).isAssignable(resultType);
419+
assert array(ANY).isAssignable(resultType) : resultType;
382420
}
383421
} else if (par0.isString() && par1.isString()) {
384422
assert snippetRun.getException() == null;
385423
TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
386-
assert STRING.isAssignable(resultType);
424+
assert STRING.isAssignable(resultType) : resultType;
387425
} else if ((par0.isNumber() || par0.isBoolean()) && (par1.isNumber() || par1.isBoolean())) {
388426
assert snippetRun.getException() == null;
389427
TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
390-
assert NUMBER.isAssignable(resultType);
428+
assert NUMBER.isAssignable(resultType) : resultType;
391429
} else {
392430
assert snippetRun.getException() != null;
393431
TypeDescriptor argType = union(STRING, BOOLEAN, NUMBER, array(ANY));
@@ -426,17 +464,17 @@ public void accept(SnippetRun snippetRun) throws PolyglotException {
426464
// string * number => string
427465
assert snippetRun.getException() == null;
428466
TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
429-
assert STRING.isAssignable(resultType);
467+
assert STRING.isAssignable(resultType) : resultType;
430468
} else if ((par0.isNumber() || par0.isBoolean()) && (par1.isNumber() || par1.isBoolean())) {
431-
// number * number has greater precendence than array * number
469+
// number * number has greater precedence than array * number
432470
assert snippetRun.getException() == null;
433471
TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
434-
assert NUMBER.isAssignable(resultType) : resultType.toString();
472+
assert NUMBER.isAssignable(resultType) : resultType;
435473
} else if (isArrayMul(par0, par1) || isArrayMul(par1, par0)) {
436474
// array * number => array
437475
assert snippetRun.getException() == null;
438476
TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
439-
assert array(ANY).isAssignable(resultType);
477+
assert array(ANY).isAssignable(resultType) : resultType;
440478
} else {
441479
assert snippetRun.getException() != null;
442480
TypeDescriptor argType = union(STRING, BOOLEAN, NUMBER, array(ANY));

0 commit comments

Comments
 (0)