Skip to content

Commit d1cfb8a

Browse files
committed
Update TCK provider to communicate that subscripting works with types on Python
1 parent a834670 commit d1cfb8a

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, 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
@@ -76,6 +76,7 @@
7676
import org.graalvm.polyglot.Value;
7777
import org.graalvm.polyglot.tck.LanguageProvider;
7878
import org.graalvm.polyglot.tck.ResultVerifier;
79+
import org.graalvm.polyglot.tck.ResultVerifier.SnippetRun;
7980
import org.graalvm.polyglot.tck.Snippet;
8081
import org.graalvm.polyglot.tck.TypeDescriptor;
8182
import org.junit.Assert;
@@ -262,8 +263,8 @@ public Collection<? extends Snippet> createExpressions(Context context) {
262263
addExpressionSnippet(context, snippets, "isinstance", "lambda x, y: isinstance(x, y)", BOOLEAN, ANY, META_OBJECT);
263264
addExpressionSnippet(context, snippets, "issubclass", "lambda x, y: issubclass(x, y)", BOOLEAN, META_OBJECT, META_OBJECT);
264265

265-
addExpressionSnippet(context, snippets, "[]", "lambda x, y: x[y]", ANY, GetItemVerifier.INSTANCE, union(array(ANY), STRING, hash(ANY, ANY)), ANY);
266-
addExpressionSnippet(context, snippets, "[a:b]", "lambda x: x[:]", union(STRING, array(ANY)), union(STRING, array(ANY)));
266+
addExpressionSnippet(context, snippets, "[]", "lambda x, y: x[y]", ANY, GetItemVerifier.INSTANCE, union(array(ANY), STRING, hash(ANY, ANY), META_OBJECT), ANY);
267+
addExpressionSnippet(context, snippets, "[a:b]", "lambda x: x[:]", union(STRING, array(ANY)), SliceVerifier.INSTANCE, union(STRING, array(ANY), META_OBJECT));
267268

268269
// @formatter:on
269270
return snippets;
@@ -493,6 +494,30 @@ public void accept(SnippetRun snippetRun) throws PolyglotException {
493494
private static final MulVerifier INSTANCE = new MulVerifier();
494495
}
495496

497+
private static void checkTypeSubscripting(SnippetRun snippetRun, Value self) {
498+
if (snippetRun.getException() != null) {
499+
// only specific Python types allow parameterization
500+
String metaName = self.getMetaQualifiedName();
501+
assert metaName.equals("object") || metaName.equals("type_user") : metaName;
502+
} else {
503+
assert snippetRun.getResult().getMetaObject().getMetaQualifiedName().equals("types.GenericAlias");
504+
}
505+
}
506+
507+
private static class SliceVerifier extends PResultVerifier {
508+
@Override
509+
public void accept(SnippetRun snippetRun) throws PolyglotException {
510+
Value self = snippetRun.getParameters().get(0);
511+
if (self.isMetaObject()) {
512+
checkTypeSubscripting(snippetRun, self);
513+
} else {
514+
ResultVerifier.getDefaultResultVerifier().accept(snippetRun);
515+
}
516+
}
517+
518+
private static final SliceVerifier INSTANCE = new SliceVerifier();
519+
}
520+
496521
private static class GetItemVerifier extends PResultVerifier {
497522
private static final String[] UNHASHABLE_TYPES = new String[]{"list", "dict", "bytearray", "set"};
498523

@@ -542,6 +567,8 @@ public void accept(SnippetRun snippetRun) throws PolyglotException {
542567
} else {
543568
assert snippetRun.getException() == null : snippetRun.getException();
544569
}
570+
} else if (self.isMetaObject()) {
571+
checkTypeSubscripting(snippetRun, self);
545572
} else {
546573
// argument type error, rethrow
547574
throw snippetRun.getException();

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import com.oracle.graal.python.nodes.PGuards;
6262
import com.oracle.graal.python.nodes.PNodeWithContext;
6363
import com.oracle.graal.python.nodes.PRaiseNode;
64-
import com.oracle.graal.python.nodes.object.IsForeignObjectNode;
6564
import com.oracle.graal.python.runtime.PythonContext;
6665
import com.oracle.graal.python.runtime.exception.PException;
6766
import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
@@ -410,12 +409,11 @@ static PTuple doNative(@SuppressWarnings("unused") Node inliningTarget, PythonAb
410409
return (PTuple) noValueToNone(readObject.readFromObj(exception, CFields.PyBaseExceptionObject__args));
411410
}
412411

413-
@Specialization
414-
static PTuple doInterop(AbstractTruffleException exception,
412+
@Specialization(guards = "isForeignObject(exception)")
413+
static PTuple doInterop(Object exception,
415414
@Bind PythonLanguage language,
416415
@CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") InteropLibrary interop,
417416
@Cached TruffleString.SwitchEncodingNode switchEncodingNode) {
418-
assert IsForeignObjectNode.executeUncached(exception);
419417
try {
420418
TruffleString message;
421419
if (interop.hasExceptionMessage(exception)) {

0 commit comments

Comments
 (0)