Skip to content

Commit 6f82cd9

Browse files
committed
print foreign arrays like python lists
1 parent c769eab commit 6f82cd9

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/foreign/TruffleObjectBuiltins.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import com.oracle.graal.python.builtins.objects.PNotImplemented;
7171
import com.oracle.graal.python.builtins.objects.foreign.TruffleObjectBuiltinsFactory.MulNodeFactory;
7272
import com.oracle.graal.python.builtins.objects.function.PKeyword;
73+
import com.oracle.graal.python.builtins.objects.iterator.PForeignArrayIterator;
7374
import com.oracle.graal.python.builtins.objects.list.PList;
7475
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins;
7576
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltinsFactory;
@@ -79,13 +80,15 @@
7980
import com.oracle.graal.python.nodes.expression.BinaryArithmetic;
8081
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
8182
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
83+
import com.oracle.graal.python.nodes.expression.CastToListNode;
8284
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
8385
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
8486
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
8587
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
8688
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
8789
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
8890
import com.oracle.graal.python.nodes.interop.PTypeToForeignNode;
91+
import com.oracle.graal.python.runtime.exception.PException;
8992
import com.oracle.graal.python.runtime.exception.PythonErrorType;
9093
import com.oracle.truffle.api.CompilerDirectives;
9194
import com.oracle.truffle.api.dsl.Cached;
@@ -957,16 +960,36 @@ protected Object doIt(TruffleObject object) {
957960
abstract static class StrNode extends UnboxNode {
958961
@Child private LookupAndCallUnaryNode callStrNode;
959962
@Child private ObjectBuiltins.StrNode objectStrNode;
963+
@Child private Node getSizeNode;
964+
@Child private Node readNode;
960965

961-
@Specialization(guards = "isForeignObject(object)")
962-
protected Object doIt(TruffleObject object) {
963-
if (isBoxed(object)) {
964-
try {
965-
return getCallStrNode().executeObject(unboxLeft(object));
966-
} catch (UnsupportedMessageException e) {
967-
throw new IllegalStateException("The object '%s' claims to be boxed, but does not support the UNBOX message");
966+
@Specialization(guards = {"isBoxed(object)"})
967+
protected Object doBoxed(TruffleObject object) {
968+
try {
969+
return getCallStrNode().executeObject(unboxLeft(object));
970+
} catch (UnsupportedMessageException e) {
971+
throw new IllegalStateException("The object '%s' claims to be boxed, but does not support the UNBOX message");
972+
}
973+
}
974+
975+
@Specialization(guards = {"isForeignArray(object)"})
976+
protected Object doArray(TruffleObject object,
977+
@Cached("create()") CastToListNode asList,
978+
@Cached("GET_SIZE.createNode()") Node sizeNode) {
979+
try {
980+
Object size = ForeignAccess.sendGetSize(sizeNode, object);
981+
if (size instanceof Integer) {
982+
PForeignArrayIterator iterable = factory().createForeignArrayIterator(object, (int) size);
983+
return getCallStrNode().executeObject(asList.executeWith(iterable));
968984
}
985+
} catch (PException | UnsupportedMessageException e) {
986+
// fall through
969987
}
988+
return doIt(object);
989+
}
990+
991+
@Fallback
992+
protected Object doIt(Object object) {
970993
return getObjectStrNode().execute(object);
971994
}
972995

0 commit comments

Comments
 (0)