Skip to content

Commit 94db775

Browse files
committed
Make __str__ call toDisplayString for foreign objects
Fixes #245
1 parent 9fed5e0 commit 94db775

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

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

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,6 @@ protected static long doIt(Object object,
10301030
abstract static class StrNode extends PythonUnaryBuiltinNode {
10311031
@Child private LookupAndCallUnaryNode callStrNode;
10321032
@Child private CastToListNode castToListNode;
1033-
@Child private ObjectNodes.DefaultObjectReprNode defaultReprNode;
10341033

10351034
@Specialization
10361035
Object str(VirtualFrame frame, Object object,
@@ -1042,7 +1041,7 @@ Object str(VirtualFrame frame, Object object,
10421041
@Cached BranchProfile isLong,
10431042
@Cached BranchProfile isDouble,
10441043
@Cached BranchProfile isArray,
1045-
@Cached BranchProfile isHostObject) {
1044+
@Cached BranchProfile defaultCase) {
10461045
try {
10471046
if (lib.isNull(object)) {
10481047
isNull.enter();
@@ -1100,31 +1099,12 @@ Object str(VirtualFrame frame, Object object,
11001099
PForeignArrayIterator iterable = factory().createForeignArrayIterator(object);
11011100
return getCallStrNode().executeObject(frame, getCastToListNode().execute(frame, iterable));
11021101
}
1103-
} else if (getContext().getEnv().isHostObject(object)) {
1104-
isHostObject.enter();
1105-
boolean isMetaObject = lib.isMetaObject(object);
1106-
Object metaObject = isMetaObject
1107-
? object
1108-
: lib.hasMetaObject(object) ? lib.getMetaObject(object) : null;
1109-
if (metaObject != null) {
1110-
Object displayName = lib.toDisplayString(metaObject);
1111-
String text = createDisplayName(isMetaObject, displayName);
1112-
return PythonUtils.format("<%s at 0x%x>", text, PythonAbstractObject.systemHashCode(object));
1113-
}
11141102
}
11151103
} catch (UnsupportedMessageException e) {
11161104
// Fall back to the generic impl
11171105
}
1118-
return defaultRepr(frame, object);
1119-
}
1120-
1121-
@TruffleBoundary
1122-
private static String createDisplayName(boolean isMetaObject, Object object) {
1123-
StringBuilder sb = new StringBuilder();
1124-
sb.append(isMetaObject ? "JavaClass[" : "JavaObject[");
1125-
sb.append(object.toString());
1126-
sb.append("]");
1127-
return sb.toString();
1106+
defaultCase.enter();
1107+
return defaultConversion(frame, lib, object);
11281108
}
11291109

11301110
private LookupAndCallUnaryNode getCallStrNode() {
@@ -1143,19 +1123,50 @@ private CastToListNode getCastToListNode() {
11431123
return castToListNode;
11441124
}
11451125

1146-
protected String defaultRepr(VirtualFrame frame, Object object) {
1147-
if (defaultReprNode == null) {
1148-
CompilerDirectives.transferToInterpreterAndInvalidate();
1149-
defaultReprNode = insert(ObjectNodes.DefaultObjectReprNode.create());
1126+
protected String defaultConversion(@SuppressWarnings("unused") VirtualFrame frame, InteropLibrary lib, Object object) {
1127+
try {
1128+
return lib.asString(lib.toDisplayString(object));
1129+
} catch (UnsupportedMessageException e) {
1130+
throw CompilerDirectives.shouldNotReachHere("toDisplayString result not convertible to String");
11501131
}
1151-
return defaultReprNode.execute(frame, object);
11521132
}
11531133
}
11541134

11551135
@Builtin(name = __REPR__, minNumOfPositionalArgs = 1)
11561136
@GenerateNodeFactory
11571137
abstract static class ReprNode extends StrNode {
1158-
protected final String method = __REPR__;
1138+
@Child private ObjectNodes.DefaultObjectReprNode defaultReprNode;
1139+
1140+
protected String defaultConversion(VirtualFrame frame, @SuppressWarnings("unused") InteropLibrary lib, Object object) {
1141+
try {
1142+
if (getContext().getEnv().isHostObject(object)) {
1143+
boolean isMetaObject = lib.isMetaObject(object);
1144+
Object metaObject = null;
1145+
if (isMetaObject) {
1146+
metaObject = object;
1147+
} else if (lib.hasMetaObject(object)) {
1148+
metaObject = lib.getMetaObject(object);
1149+
}
1150+
if (metaObject != null) {
1151+
Object displayName = lib.toDisplayString(metaObject);
1152+
String text = createDisplayName(isMetaObject, displayName);
1153+
return PythonUtils.format("<%s at 0x%x>", text, PythonAbstractObject.systemHashCode(object));
1154+
}
1155+
}
1156+
} catch (UnsupportedMessageException e) {
1157+
// fallthrough to default
1158+
}
1159+
if (defaultReprNode == null) {
1160+
CompilerDirectives.transferToInterpreterAndInvalidate();
1161+
defaultReprNode = insert(ObjectNodes.DefaultObjectReprNode.create());
1162+
}
1163+
return defaultReprNode.execute(frame, object);
1164+
}
1165+
1166+
@TruffleBoundary
1167+
private static String createDisplayName(boolean isMetaObject, Object object) {
1168+
return (isMetaObject ? "JavaClass[" : "JavaObject[") + object + "]";
1169+
}
11591170
}
11601171

11611172
@Builtin(name = __BASES__, minNumOfPositionalArgs = 1, isGetter = true, isSetter = false)

0 commit comments

Comments
 (0)