|
1 | 1 | /* |
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * The Universal Permissive License (UPL), Version 1.0 |
|
76 | 76 | import org.graalvm.polyglot.Value; |
77 | 77 | import org.graalvm.polyglot.tck.LanguageProvider; |
78 | 78 | import org.graalvm.polyglot.tck.ResultVerifier; |
| 79 | +import org.graalvm.polyglot.tck.ResultVerifier.SnippetRun; |
79 | 80 | import org.graalvm.polyglot.tck.Snippet; |
80 | 81 | import org.graalvm.polyglot.tck.TypeDescriptor; |
81 | 82 | import org.junit.Assert; |
@@ -262,8 +263,8 @@ public Collection<? extends Snippet> createExpressions(Context context) { |
262 | 263 | addExpressionSnippet(context, snippets, "isinstance", "lambda x, y: isinstance(x, y)", BOOLEAN, ANY, META_OBJECT); |
263 | 264 | addExpressionSnippet(context, snippets, "issubclass", "lambda x, y: issubclass(x, y)", BOOLEAN, META_OBJECT, META_OBJECT); |
264 | 265 |
|
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)); |
267 | 268 |
|
268 | 269 | // @formatter:on |
269 | 270 | return snippets; |
@@ -493,6 +494,30 @@ public void accept(SnippetRun snippetRun) throws PolyglotException { |
493 | 494 | private static final MulVerifier INSTANCE = new MulVerifier(); |
494 | 495 | } |
495 | 496 |
|
| 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 | + |
496 | 521 | private static class GetItemVerifier extends PResultVerifier { |
497 | 522 | private static final String[] UNHASHABLE_TYPES = new String[]{"list", "dict", "bytearray", "set"}; |
498 | 523 |
|
@@ -542,6 +567,8 @@ public void accept(SnippetRun snippetRun) throws PolyglotException { |
542 | 567 | } else { |
543 | 568 | assert snippetRun.getException() == null : snippetRun.getException(); |
544 | 569 | } |
| 570 | + } else if (self.isMetaObject()) { |
| 571 | + checkTypeSubscripting(snippetRun, self); |
545 | 572 | } else { |
546 | 573 | // argument type error, rethrow |
547 | 574 | throw snippetRun.getException(); |
|
0 commit comments