Skip to content

Commit 0b1d2e4

Browse files
committed
fix more expectations of TCK
1 parent 80d0a04 commit 0b1d2e4

File tree

2 files changed

+16
-156
lines changed

2 files changed

+16
-156
lines changed

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

Lines changed: 5 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public class PythonProvider implements LanguageProvider {
9494
private static final TypeDescriptor DICT = dict(ANY, ANY);
9595
private static final TypeDescriptor LIST = list(ANY);
9696
private static final TypeDescriptor TUPLE = tuple(ANY);
97-
private static final TypeDescriptor NON_INSTANTIABLE_TYPE = intersection(OBJECT, META_OBJECT);
9897
private static final TypeDescriptor DATETIME_DATE = intersection(OBJECT, DATE);
9998
private static final TypeDescriptor DATETIME_TIME = intersection(OBJECT, TIME);
10099
private static final TypeDescriptor DATETIME_DATETIME = intersection(OBJECT, DATE, TIME);
@@ -103,7 +102,7 @@ public class PythonProvider implements LanguageProvider {
103102
private static final TypeDescriptor BASE_EXCEPTION = intersection(OBJECT, EXCEPTION);
104103

105104
private static final TypeDescriptor type(TypeDescriptor instance, boolean varargs, TypeDescriptor... params) {
106-
return intersection(OBJECT, instantiable(instance, varargs, params), META_OBJECT);
105+
return intersection(OBJECT, instantiable(instance, varargs, params), executable(instance, varargs, params), META_OBJECT);
107106
}
108107

109108
private static final TypeDescriptor list(TypeDescriptor componentType) {
@@ -219,14 +218,11 @@ public Collection<? extends Snippet> createExpressions(Context context) {
219218

220219
// @formatter:off
221220
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", NUMBER, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
222-
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", array(ANY), PNoListCoercionVerifier.INSTANCE, array(ANY), array(ANY));
221+
addExpressionSnippet(context, snippets, "+ str", "lambda x, y: x + y", NUMBER, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
222+
addExpressionSnippet(context, snippets, "+ list", "lambda x, y: x + y", array(ANY), PNoListCoercionVerifier.INSTANCE, array(ANY), array(ANY));
223223

224224
addExpressionSnippet(context, snippets, "-", "lambda x, y: x - y", NUMBER, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
225225

226-
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", NUMBER, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
227-
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(LIST, STRING, STR), PSequenceMultiplicationVerifier.INSTANCE, array(ANY), union(BOOLEAN, NUMBER));
228-
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(LIST, STRING, STR), PSequenceMultiplicationVerifier.INSTANCE, union(BOOLEAN, NUMBER), array(ANY));
229-
230226
addExpressionSnippet(context, snippets, "/", "lambda x, y: x / y", NUMBER, PDivByZeroVerifier.INSTANCE, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
231227

232228
addExpressionSnippet(context, snippets, "list-from-foreign", "lambda x: list(x)", array(ANY), union(STRING, iterable(ANY), iterator(ANY), array(ANY)));
@@ -238,7 +234,7 @@ public Collection<? extends Snippet> createExpressions(Context context) {
238234
addExpressionSnippet(context, snippets, "<", "lambda x, y: x < y", BOOLEAN, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
239235
addExpressionSnippet(context, snippets, "<=", "lambda x, y: x <= y", BOOLEAN, union(BOOLEAN, NUMBER), union(BOOLEAN, NUMBER));
240236

241-
addExpressionSnippet(context, snippets, "isinstance", "lambda x, y: isinstance(x, y)", BOOLEAN, OBJECT, META_OBJECT);
237+
addExpressionSnippet(context, snippets, "isinstance", "lambda x, y: isinstance(x, y)", BOOLEAN, ANY, META_OBJECT);
242238
addExpressionSnippet(context, snippets, "issubclass", "lambda x, y: issubclass(x, y)", BOOLEAN, META_OBJECT, META_OBJECT);
243239
// @formatter:on
244240
return snippets;
@@ -267,7 +263,7 @@ public Collection<? extends Snippet> createStatements(Context context) {
267263
addStatementSnippet(context, snippets, "for", "def gen_for(l):\n" +
268264
" for x in l:\n" +
269265
" return x\n\n" +
270-
"gen_for", ANY, union(array(ANY), iterable(ANY), iterator(ANY)));
266+
"gen_for", ANY, union(array(ANY), iterable(ANY), iterator(ANY), STRING));
271267

272268
// any exception honours the finally block, but non-exception cannot be raised
273269
addStatementSnippet(context, snippets, "try-finally", "def gen_tryfinally(exc):\n" +
@@ -345,87 +341,6 @@ private static Source createSource(String resourceName) throws IOException {
345341
private abstract static class PResultVerifier implements ResultVerifier {
346342
}
347343

348-
private static class PSequenceMultiplicationVerifier extends PResultVerifier {
349-
350-
public void accept(SnippetRun snippetRun) throws PolyglotException {
351-
List<? extends Value> parameters = snippetRun.getParameters();
352-
assert parameters.size() == 2;
353-
354-
Value par0 = parameters.get(0);
355-
Value par1 = parameters.get(1);
356-
357-
// Just restrict 'number' to integer value space
358-
if (isSequence(par0) && isNumber(par1)) {
359-
if (!hasMemoryError(snippetRun)) {
360-
if (isInteger(par1) || isNegativeNumber(par1)) {
361-
ResultVerifier.getDefaultResultVerifier().accept(snippetRun);
362-
} else {
363-
if (snippetRun.getException() == null) {
364-
throw new AssertionError("<sequence> * <non-integer> should give an error.");
365-
}
366-
}
367-
}
368-
} else if (isNumber(par0) && isSequence(par1)) {
369-
if (!hasMemoryError(snippetRun)) {
370-
if (isInteger(par0) || isNegativeNumber(par0)) {
371-
ResultVerifier.getDefaultResultVerifier().accept(snippetRun);
372-
} else {
373-
if (snippetRun.getException() == null) {
374-
throw new AssertionError("<non-integer> * <sequence> should give an error.");
375-
}
376-
}
377-
}
378-
} else if (isNumber(par0) && isMapping(par1) || isNumber(par1) && isMapping(par0)) {
379-
if (snippetRun.getException() != null) {
380-
throw new AssertionError("Multipliation with mapping should give an error.");
381-
}
382-
} else if (isSequence(par0) && isSequence(par1)) {
383-
if (snippetRun.getException() == null) {
384-
throw new AssertionError("<sequence> * <sequence> should give an error.");
385-
} else {
386-
throw snippetRun.getException();
387-
}
388-
} else if (!(isNumber(par0) && isScalarVector(par1) || isScalarVector(par0) && isNumber(par1))) {
389-
ResultVerifier.getDefaultResultVerifier().accept(snippetRun);
390-
}
391-
}
392-
393-
protected static boolean isScalarVector(Value val) {
394-
return isNumber(val) && val.hasArrayElements() && val.getArraySize() == 1 && !isMapping(val);
395-
}
396-
397-
protected static boolean isNumber(Value par0) {
398-
return par0.isNumber() || par0.isBoolean();
399-
}
400-
401-
protected static boolean isNegativeNumber(Value par0) {
402-
return par0.isNumber() && par0.fitsInLong() && par0.asLong() < 0L;
403-
}
404-
405-
protected static boolean isSequence(Value par0) {
406-
return !isNumber(par0) && (par0.isString() || (par0.hasArrayElements() && !isMapping(par0)));
407-
}
408-
409-
protected static boolean isMapping(Value par0) {
410-
return par0.hasMembers() && par0.getMetaObject().toString().contains("dict");
411-
}
412-
413-
private static boolean hasMemoryError(SnippetRun snippetRun) {
414-
PolyglotException exception = snippetRun.getException();
415-
if (exception != null && exception.isGuestException()) {
416-
return "MemoryError".equals(exception.getMessage()) || exception.getMessage().contains("OverflowError");
417-
}
418-
return false;
419-
}
420-
421-
private static boolean isInteger(Value par0) {
422-
return (par0.isNumber() && par0.fitsInInt() || par0.isBoolean());
423-
}
424-
425-
private static final PSequenceMultiplicationVerifier INSTANCE = new PSequenceMultiplicationVerifier();
426-
427-
}
428-
429344
/**
430345
* Only accepts exact matches of types.
431346
*/
@@ -452,35 +367,6 @@ public void accept(SnippetRun snippetRun) throws PolyglotException {
452367
private static final PNoListCoercionVerifier INSTANCE = new PNoListCoercionVerifier();
453368
}
454369

455-
/**
456-
* Foreign objects may be array-ish and boxed (e.g. if they have just one element). In this
457-
* case, we still treat them as arrays.
458-
*/
459-
private static class PNoArrayVerifier extends PResultVerifier {
460-
461-
public void accept(SnippetRun snippetRun) throws PolyglotException {
462-
List<? extends Value> parameters = snippetRun.getParameters();
463-
assert parameters.size() == 2;
464-
465-
Value par0 = parameters.get(0);
466-
Value par1 = parameters.get(1);
467-
468-
if (isNumber(par0) && isNumber(par1)) {
469-
if (!(par0.hasArrayElements() || par1.hasArrayElements())) {
470-
ResultVerifier.getDefaultResultVerifier().accept(snippetRun);
471-
}
472-
} else {
473-
ResultVerifier.getDefaultResultVerifier().accept(snippetRun);
474-
}
475-
}
476-
477-
private static boolean isNumber(Value val) {
478-
return val.isNumber() || val.isBoolean();
479-
}
480-
481-
private static final PNoArrayVerifier INSTANCE = new PNoArrayVerifier();
482-
}
483-
484370
/**
485371
* Only accepts exact matches of types.
486372
*/
@@ -501,41 +387,4 @@ public void accept(SnippetRun snippetRun) throws PolyglotException {
501387

502388
private static final PDivByZeroVerifier INSTANCE = new PDivByZeroVerifier();
503389
}
504-
505-
/**
506-
* Only accepts exact matches of types.
507-
*/
508-
private static class PDictMemberVerifier extends PResultVerifier {
509-
510-
private final String[] expectedMembers;
511-
private final String[] unexpectedMembers;
512-
513-
public PDictMemberVerifier(String[] expectedMembers, String[] unexpectedMembers) {
514-
this.expectedMembers = expectedMembers;
515-
this.unexpectedMembers = unexpectedMembers;
516-
}
517-
518-
public void accept(SnippetRun snippetRun) throws PolyglotException {
519-
ResultVerifier.getDefaultResultVerifier().accept(snippetRun);
520-
521-
Value result = snippetRun.getResult();
522-
if (result.hasMembers()) {
523-
for (String expectedMember : expectedMembers) {
524-
if (!result.hasMember(expectedMember)) {
525-
throw new AssertionError("Expected member missing: " + expectedMember);
526-
}
527-
}
528-
for (String unexpectedMember : unexpectedMembers) {
529-
if (result.hasMember(unexpectedMember)) {
530-
throw new AssertionError("Unexpected member present: " + unexpectedMember);
531-
}
532-
}
533-
}
534-
}
535-
}
536-
537-
private static String[] arr(String... strings) {
538-
return strings;
539-
}
540-
541390
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/iterator/IteratorNodes.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@
5858
import com.oracle.graal.python.nodes.call.special.LookupSpecialMethodNode;
5959
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6060
import com.oracle.graal.python.runtime.exception.PException;
61+
import com.oracle.truffle.api.CompilerDirectives;
6162
import com.oracle.truffle.api.dsl.Cached;
6263
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6364
import com.oracle.truffle.api.dsl.GenerateUncached;
6465
import com.oracle.truffle.api.dsl.ImportStatic;
6566
import com.oracle.truffle.api.dsl.Specialization;
6667
import com.oracle.truffle.api.frame.VirtualFrame;
68+
import com.oracle.truffle.api.interop.InteropLibrary;
69+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
6770
import com.oracle.truffle.api.library.CachedLibrary;
6871
import com.oracle.truffle.api.nodes.Node;
6972
import com.oracle.truffle.api.profiles.ConditionProfile;
@@ -94,6 +97,7 @@ static int length(@SuppressWarnings({"unused"}) VirtualFrame frame, @SuppressWar
9497

9598
@Specialization(guards = {"!isNoValue(iterable)", "!isString(iterable)"}, limit = "4")
9699
int length(VirtualFrame frame, Object iterable,
100+
@CachedLibrary("iterable") InteropLibrary iLib,
97101
@CachedLibrary("iterable") PythonObjectLibrary plib,
98102
@CachedLibrary(limit = "3") PythonObjectLibrary toInt,
99103
@Cached("create(__LEN__)") LookupAttributeInMRONode lenNode,
@@ -103,6 +107,13 @@ int length(VirtualFrame frame, Object iterable,
103107
@Cached ConditionProfile hasLenProfile,
104108
@Cached ConditionProfile hasLengthHintProfile,
105109
@Cached PRaiseNode raiseNode) {
110+
if (iLib.isString(iterable)) {
111+
try {
112+
return iLib.asString(iterable).length();
113+
} catch (UnsupportedMessageException e) {
114+
throw CompilerDirectives.shouldNotReachHere();
115+
}
116+
}
106117
Object clazz = plib.getLazyPythonClass(iterable);
107118
Object attrLenObj = lenNode.execute(clazz);
108119
if (hasLenProfile.profile(attrLenObj != PNone.NO_VALUE)) {

0 commit comments

Comments
 (0)