Skip to content

Commit f991e52

Browse files
committed
[GR-21265] In EmulateJython mode isinstance function should behave in different way.
PullRequest: graalpython/822
2 parents 53f795a + 5b08e5e commit f991e52

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-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
@@ -490,3 +490,17 @@ def test_java_null_is_none():
490490
assert (x is z) == False
491491
assert x is not z
492492

493+
def test_isinstance01():
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+
499+
def test_isinstance02():
500+
if sys.graal_python_jython_emulation_enabled:
501+
import java.util.Map as Map
502+
import java.util.HashMap as HashMap
503+
h = HashMap()
504+
assert isinstance(h, HashMap)
505+
assert isinstance(h, Map)
506+

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
181181
import com.oracle.truffle.api.RootCallTarget;
182182
import com.oracle.truffle.api.Truffle;
183+
import com.oracle.truffle.api.TruffleLanguage;
183184
import com.oracle.truffle.api.debug.Debugger;
184185
import com.oracle.truffle.api.dsl.Cached;
185186
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -1056,6 +1057,8 @@ public abstract static class IsInstanceNode extends PythonBinaryBuiltinNode {
10561057
@Child private SequenceStorageNodes.LenNode lenNode;
10571058
@Child private GetObjectArrayNode getObjectArrayNode;
10581059

1060+
@CompilationFinal private Boolean emulateJython;
1061+
10591062
public static IsInstanceNode create() {
10601063
return BuiltinFunctionsFactory.IsInstanceNodeFactory.create();
10611064
}
@@ -1101,8 +1104,22 @@ boolean isInstance(VirtualFrame frame, Object instance, PTuple clsTuple,
11011104
return false;
11021105
}
11031106

1107+
protected boolean emulateJython() {
1108+
if (emulateJython == null) {
1109+
CompilerDirectives.transferToInterpreterAndInvalidate();
1110+
emulateJython = PythonOptions.getFlag(getContext(), PythonOptions.EmulateJython);
1111+
}
1112+
return emulateJython;
1113+
}
1114+
11041115
@Fallback
11051116
boolean isInstance(VirtualFrame frame, Object instance, Object cls) {
1117+
TruffleLanguage.Env env = getContext().getEnv();
1118+
if (emulateJython() && env.isHostObject(cls)) {
1119+
Object hostCls = env.asHostObject(cls);
1120+
Object hostInstance = env.isHostObject(instance) ? env.asHostObject(instance) : instance;
1121+
return hostCls instanceof Class && ((Class<?>) hostCls).isAssignableFrom(hostInstance.getClass());
1122+
}
11061123
return isInstanceCheckInternal(frame, instance, cls) || typeInstanceCheckNode.executeWith(frame, cls, instance);
11071124
}
11081125

0 commit comments

Comments
 (0)