Skip to content

Commit 4d9612b

Browse files
committed
[GR-29785] JavaClass should print better string then <foreign object at xxxxx>
PullRequest: graalpython/1668
2 parents 8f8e54b + 4d64538 commit 4d9612b

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,17 @@ def test_foreign_slice_setting():
638638
assert True
639639
else:
640640
assert False, "should throw a type error again"
641+
642+
def test_foreign_repl():
643+
from java.util.logging import LogRecord
644+
from java.util.logging import Level
645+
646+
lr = LogRecord(Level.ALL, "message")
647+
assert repr(LogRecord).startswith('<JavaClass[java.util.logging.LogRecord] at')
648+
assert repr(lr).startswith('<JavaObject[java.util.logging.LogRecord] at')
649+
650+
from java.lang import Integer
651+
i = Integer('22')
652+
assert repr(Integer).startswith('<JavaClass[java.lang.Integer] at')
653+
assert repr(i) == '22'
654+

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import com.oracle.graal.python.builtins.PythonBuiltins;
8383
import com.oracle.graal.python.builtins.objects.PNone;
8484
import com.oracle.graal.python.builtins.objects.PNotImplemented;
85+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
8586
import com.oracle.graal.python.builtins.objects.function.PKeyword;
8687
import com.oracle.graal.python.builtins.objects.ints.PInt;
8788
import com.oracle.graal.python.builtins.objects.iterator.PForeignArrayIterator;
@@ -876,6 +877,35 @@ protected Object doArray(VirtualFrame frame, Object object,
876877
return doIt(frame, object);
877878
}
878879

880+
@Specialization(guards = "getContext().getEnv().isHostObject(self)")
881+
Object doHostObject(VirtualFrame frame, Object self,
882+
@CachedLibrary(limit = "3") InteropLibrary lib) {
883+
try {
884+
boolean isMetaObject = lib.isMetaObject(self);
885+
Object metaObject = isMetaObject
886+
? self
887+
: lib.hasMetaObject(self) ? lib.getMetaObject(self) : null;
888+
if (metaObject != null) {
889+
Object displayName = lib.toDisplayString(metaObject);
890+
String text = createDisplayName(isMetaObject, displayName);
891+
return PythonUtils.format("<%s at 0x%x>", text, PythonAbstractObject.systemHashCode(self));
892+
}
893+
894+
} catch (UnsupportedMessageException ex) {
895+
// do nothing
896+
}
897+
return doIt(frame, self);
898+
}
899+
900+
@CompilerDirectives.TruffleBoundary
901+
private static String createDisplayName(boolean isMetaObject, Object object) {
902+
StringBuilder sb = new StringBuilder();
903+
sb.append(isMetaObject ? "JavaClass[" : "JavaObject[");
904+
sb.append(object.toString());
905+
sb.append("]");
906+
return sb.toString();
907+
}
908+
879909
@Fallback
880910
protected Object doIt(VirtualFrame frame, Object object) {
881911
return getObjectStrNode().call(frame, object);

0 commit comments

Comments
 (0)