Skip to content

Commit d97e144

Browse files
committed
Add some tests for foreign strings repetition
1 parent 401033f commit d97e144

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ public Collection<? extends Snippet> createExpressions(Context context) {
235235
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, array(ANY)), AddVerifier.INSTANCE, numeric, union(STRING, array(ANY)));
236236
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, array(ANY)), AddVerifier.INSTANCE, union(STRING, array(ANY)), numeric);
237237
addExpressionSnippet(context, snippets, "+", "lambda x, y: x + y", union(STRING, array(ANY)), AddVerifier.INSTANCE, union(STRING, array(ANY)), union(STRING, array(ANY)));
238+
238239
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", NUMBER, new NonPrimitiveNumberParameterThrows(MulVerifier.INSTANCE), numeric, numeric);
239240
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(STRING, array(ANY)), MulVerifier.INSTANCE, numeric, union(STRING, array(ANY)));
240241
addExpressionSnippet(context, snippets, "*", "lambda x, y: x * y", union(STRING, array(ANY)), MulVerifier.INSTANCE, union(STRING, array(ANY)), numeric);

graalpython/com.oracle.graal.python.test/src/tests/test_interop.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,14 @@ def test_foreign_number_list(self):
10691069
assert l > n
10701070
assert n < l
10711071

1072+
def test_foreign_string(self):
1073+
s = __graalpython__.foreign_wrapper("ab")
1074+
1075+
self.assertEqual(['ForeignString', 'foreign', 'object'], [t.__name__ for t in type(s).mro()])
1076+
1077+
assert s * 3 == "ababab"
1078+
assert 3 * s == "ababab"
1079+
10721080
def test_foreign_repl(self):
10731081
from java.util.logging import LogRecord
10741082
from java.util.logging import Level

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ public void postInitialize(Python3Core core) {
279279
mod.setAttribute(tsLiteral("is_strong_handle_table_ref"), PNone.NO_VALUE);
280280
mod.setAttribute(tsLiteral("clear_interop_type_registry"), PNone.NO_VALUE);
281281
mod.setAttribute(tsLiteral("foreign_number_list"), PNone.NO_VALUE);
282+
mod.setAttribute(tsLiteral("foreign_wrapper"), PNone.NO_VALUE);
282283
}
283284
if (PythonImageBuildOptions.WITHOUT_PLATFORM_ACCESS || !context.getOption(PythonOptions.RunViaLauncher)) {
284285
mod.setAttribute(tsLiteral("list_files"), PNone.NO_VALUE);
@@ -1191,4 +1192,24 @@ long getArraySize() {
11911192
}
11921193
}
11931194
}
1195+
1196+
@Builtin(name = "foreign_wrapper", maxNumOfPositionalArgs = 1)
1197+
@GenerateNodeFactory
1198+
public abstract static class ForeignWrapperNode extends PythonBuiltinNode {
1199+
1200+
@Specialization
1201+
@TruffleBoundary
1202+
Object foreignWrapper(Object object) {
1203+
return new ForeignWrapper(object);
1204+
}
1205+
1206+
@ExportLibrary(value = InteropLibrary.class, delegateTo = "object")
1207+
static final class ForeignWrapper implements TruffleObject {
1208+
final Object object;
1209+
1210+
ForeignWrapper(Object object) {
1211+
this.object = object;
1212+
}
1213+
}
1214+
}
11941215
}

0 commit comments

Comments
 (0)