Skip to content

Commit 11e9354

Browse files
committed
[GR-15411] Don't print TruffleObjects on AttributeError
PullRequest: graalpython/498
2 parents 95fe2fb + 880679c commit 11e9354

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,34 @@ def test_java_imports():
339339
assert repr(ArrayList()) == "[]"
340340

341341
assert java.util.ArrayList == ArrayList
342+
343+
def test_foreign_object_does_not_leak_Javas_toString():
344+
try:
345+
from java.util import ArrayList
346+
except NotImplementedError as e:
347+
assert "host lookup is not allowed" in str(e)
348+
else:
349+
try:
350+
ArrayList(12, "12")
351+
except TypeError as e:
352+
assert "@" not in str(e) # the @ from Java's default toString
353+
354+
try:
355+
ArrayList(12, foo="12") # keywords are not supported
356+
except TypeError as e:
357+
assert "@" not in str(e) # the @ from Java's default toString
358+
359+
try:
360+
ArrayList.bar
361+
except AttributeError as e:
362+
assert "@" not in str(e) # the @ from Java's default toString
363+
364+
try:
365+
del ArrayList.bar
366+
except AttributeError as e:
367+
assert "@" not in str(e) # the @ from Java's default toString
368+
369+
try:
370+
del ArrayList.bar
371+
except AttributeError as e:
372+
assert "@" not in str(e) # the @ from Java's default toString

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ boolean doForeignObject(VirtualFrame frame, Object self,
133133
return !lib.isNull(self);
134134
}
135135
} catch (UnsupportedMessageException e) {
136-
throw raise(TypeError, "foreign.__bool__ should return a boolean value");
136+
throw raise(AttributeError, "'foreign' object has no attribute '__bool__'");
137137
}
138138
}
139139
}
@@ -172,7 +172,7 @@ public long len(Object self,
172172
} catch (UnsupportedMessageException e) {
173173
// fall through
174174
}
175-
throw raise(AttributeError, "'foreign' object has no attribute 'len'");
175+
throw raise(AttributeError, "'foreign' object has no attribute '__len__'");
176176
}
177177
}
178178

@@ -563,13 +563,14 @@ protected Object doInteropCall(Object callee, Object[] arguments, @SuppressWarni
563563
Object res = lib.instantiate(callee, convertedArgs);
564564
return toPTypeNode.executeConvert(res);
565565
} catch (ArityException | UnsupportedTypeException | UnsupportedMessageException e) {
566-
throw raise(PythonErrorType.TypeError, "invalid instantiation of foreign object %s()", callee);
566+
throw raise(PythonErrorType.TypeError, "invalid instantiation of foreign object");
567567
}
568568
}
569569

570570
@Fallback
571-
protected Object doGeneric(Object callee, @SuppressWarnings("unused") Object arguments, @SuppressWarnings("unused") Object keywords) {
572-
throw raise(PythonErrorType.TypeError, "invalid instantiation of foreign object %s()", callee);
571+
@SuppressWarnings("unused")
572+
protected Object doGeneric(Object callee, Object arguments, Object keywords) {
573+
throw raise(PythonErrorType.TypeError, "invalid instantiation of foreign object");
573574
}
574575
}
575576

@@ -609,13 +610,14 @@ protected Object doInteropCall(VirtualFrame frame, Object callee, Object[] argum
609610
}
610611
}
611612
} catch (ArityException | UnsupportedTypeException | UnsupportedMessageException e) {
612-
throw raise(PythonErrorType.TypeError, "invalid invocation of foreign callable %s()", callee);
613+
throw raise(PythonErrorType.TypeError, "invalid invocation of foreign callable");
613614
}
614615
}
615616

616617
@Fallback
617-
protected Object doGeneric(Object callee, @SuppressWarnings("unused") Object arguments, @SuppressWarnings("unused") Object keywords) {
618-
throw raise(PythonErrorType.TypeError, "invalid invocation of foreign callable %s()", callee);
618+
@SuppressWarnings("unused")
619+
protected Object doGeneric(Object callee, Object arguments, Object keywords) {
620+
throw raise(PythonErrorType.TypeError, "invalid invocation of foreign callable");
619621
}
620622

621623
public static CallNode create() {
@@ -640,16 +642,15 @@ abstract static class GetattrNode extends PythonBinaryBuiltinNode {
640642
@Child PForeignToPTypeNode toPythonNode = PForeignToPTypeNode.create();
641643

642644
@Specialization
643-
protected Object doIt(Object object, Object key,
645+
protected Object doIt(Object object, String member,
644646
@CachedLibrary(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)") InteropLibrary read) {
645647
try {
646-
String member = (String) key;
647648
if (read.isMemberReadable(object, member)) {
648649
return toPythonNode.executeConvert(read.readMember(object, member));
649650
}
650651
} catch (UnknownIdentifierException | UnsupportedMessageException ignore) {
651652
}
652-
throw raise(PythonErrorType.AttributeError, "foreign object %s has no attribute %s", object, key);
653+
throw raise(PythonErrorType.AttributeError, "foreign object has no attribute '%s'", member);
653654
}
654655
}
655656

@@ -662,7 +663,7 @@ protected PNone doIt(Object object, String key, Object value,
662663
try {
663664
lib.writeMember(object, key, value);
664665
} catch (UnknownIdentifierException | UnsupportedMessageException | UnsupportedTypeException e) {
665-
throw raise(PythonErrorType.AttributeError, "foreign object %s has no attribute %s", object, key);
666+
throw raise(PythonErrorType.AttributeError, "foreign object has no attribute '%s'", key);
666667
}
667668
return PNone.NONE;
668669
}
@@ -689,7 +690,7 @@ protected PNone doIt(Object object, String key,
689690
try {
690691
lib.removeMember(object, key);
691692
} catch (UnknownIdentifierException | UnsupportedMessageException e) {
692-
throw raise(PythonErrorType.AttributeError, "foreign object %s has no attribute %s", object, key);
693+
throw raise(PythonErrorType.AttributeError, "foreign object has no attribute '%s'", key);
693694
}
694695
return PNone.NONE;
695696
}
@@ -717,7 +718,7 @@ protected Object doIt(Object object,
717718
try {
718719
return lib.getMembers(object);
719720
} catch (UnsupportedMessageException e) {
720-
throw raise(TypeError, "The object '%s' claims to have members, but does not return them", object);
721+
throw new IllegalStateException("foreign object claims to have members, but does not return them");
721722
}
722723
} else {
723724
return factory().createList();
@@ -735,7 +736,7 @@ protected Object doIt(Object object,
735736
try {
736737
return lib.asInt(object);
737738
} catch (UnsupportedMessageException e) {
738-
throw raise(TypeError, "foreign value '%s' claims it fits into index-sized int, but doesn't", object);
739+
throw new IllegalStateException("foreign value claims it fits into index-sized int, but doesn't");
739740
}
740741
}
741742
throw raiseIndexError();
@@ -761,7 +762,7 @@ protected Object doBool(VirtualFrame frame, Object object,
761762
try {
762763
return getCallStrNode().executeObject(frame, lib.asBoolean(object));
763764
} catch (UnsupportedMessageException e) {
764-
throw new IllegalStateException("The object '%s' claims to be boxed, but does not support the appropriate unbox message");
765+
throw new IllegalStateException("foreign object claims to be boxed, but does not support the appropriate unbox message");
765766
}
766767
}
767768

@@ -771,7 +772,7 @@ protected Object doStr(VirtualFrame frame, Object object,
771772
try {
772773
return getCallStrNode().executeObject(frame, lib.asString(object));
773774
} catch (UnsupportedMessageException e) {
774-
throw new IllegalStateException("The object '%s' claims to be boxed, but does not support the appropriate unbox message");
775+
throw new IllegalStateException("foreign object claims to be boxed, but does not support the appropriate unbox message");
775776
}
776777
}
777778

@@ -781,7 +782,7 @@ protected Object doLong(VirtualFrame frame, Object object,
781782
try {
782783
return getCallStrNode().executeObject(frame, lib.asLong(object));
783784
} catch (UnsupportedMessageException e) {
784-
throw new IllegalStateException("The object '%s' claims to be boxed, but does not support the appropriate unbox message");
785+
throw new IllegalStateException("foreign object claims to be boxed, but does not support the appropriate unbox message");
785786
}
786787
}
787788

@@ -791,7 +792,7 @@ protected Object doDouble(VirtualFrame frame, Object object,
791792
try {
792793
return getCallStrNode().executeObject(frame, lib.asDouble(object));
793794
} catch (UnsupportedMessageException e) {
794-
throw new IllegalStateException("The object '%s' claims to be boxed, but does not support the appropriate unbox message");
795+
throw new IllegalStateException("foreign object claims to be boxed, but does not support the appropriate unbox message");
795796
}
796797
}
797798

0 commit comments

Comments
 (0)