Skip to content

Commit bda1640

Browse files
committed
[GR-21265] In EmulateJython mode isinstance function should behave in different way.
1 parent b4b90ed commit bda1640

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,9 @@ def test_java_null_is_none():
490490
assert (x is z) == False
491491
assert x is not z
492492

493+
def test_isinstance():
494+
if sys.graal_python_jython_emulation_enabled:
495+
import java.lang.Integer as Integer
496+
i = Integer(1)
497+
assert isinstance(i, Integer)
498+

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,8 @@ public abstract static class IsInstanceNode extends PythonBinaryBuiltinNode {
10561056
@Child private SequenceStorageNodes.LenNode lenNode;
10571057
@Child private GetObjectArrayNode getObjectArrayNode;
10581058

1059+
@CompilationFinal private Boolean emulateJython;
1060+
10591061
public static IsInstanceNode create() {
10601062
return BuiltinFunctionsFactory.IsInstanceNodeFactory.create();
10611063
}
@@ -1101,8 +1103,20 @@ boolean isInstance(VirtualFrame frame, Object instance, PTuple clsTuple,
11011103
return false;
11021104
}
11031105

1106+
protected boolean emulateJython() {
1107+
if (emulateJython == null) {
1108+
CompilerDirectives.transferToInterpreterAndInvalidate();
1109+
emulateJython = PythonOptions.getFlag(getContext(), PythonOptions.EmulateJython);
1110+
}
1111+
return emulateJython;
1112+
}
1113+
11041114
@Fallback
11051115
boolean isInstance(VirtualFrame frame, Object instance, Object cls) {
1116+
if (emulateJython() && getContext().getEnv().isHostObject(cls)) {
1117+
Object hostType = getContext().getEnv().asHostObject(cls);
1118+
return instance.getClass().isAssignableFrom((Class<?>) hostType);
1119+
}
11061120
return isInstanceCheckInternal(frame, instance, cls) || typeInstanceCheckNode.executeWith(frame, cls, instance);
11071121
}
11081122

0 commit comments

Comments
 (0)