Skip to content

Commit 09f9204

Browse files
committed
Correctly distinguish between native object and native type.
1 parent 5c859a2 commit 09f9204

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/CExtNodes.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,15 @@ Object doPythonClassUncached(ManagedPythonClass object,
334334
return PythonClassNativeWrapper.wrap(object, getNameNode.execute(object));
335335
}
336336

337-
@Specialization(guards = {"cachedClass == object.getClass()", "!isPythonClass(object)", "!isNativeObject(object)", "!isNoValue(object)"}, limit = "3")
337+
@Specialization(guards = {"cachedClass == object.getClass()", "!isClass(object)", "!isNativeObject(object)", "!isNoValue(object)"}, limit = "3")
338338
Object runAbstractObjectCached(PythonAbstractObject object,
339339
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile,
340340
@Cached("object.getClass()") Class<? extends PythonAbstractObject> cachedClass) {
341341
assert object != PNone.NO_VALUE;
342342
return PythonObjectNativeWrapper.wrap(CompilerDirectives.castExact(object, cachedClass), noWrapperProfile);
343343
}
344344

345-
@Specialization(guards = {"!isPythonClass(object)", "!isNativeObject(object)", "!isNoValue(object)"}, replaces = "runAbstractObjectCached")
345+
@Specialization(guards = {"!isClass(object)", "!isNativeObject(object)", "!isNoValue(object)"}, replaces = "runAbstractObjectCached")
346346
Object runAbstractObject(PythonAbstractObject object,
347347
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile) {
348348
assert object != PNone.NO_VALUE;
@@ -360,10 +360,6 @@ Object run(Object obj) {
360360
return obj;
361361
}
362362

363-
protected static boolean isPythonClass(PythonAbstractObject o) {
364-
return o instanceof AbstractPythonClass;
365-
}
366-
367363
protected static PythonClassNativeWrapper wrapNativeClass(ManagedPythonClass object) {
368364
return PythonClassNativeWrapper.wrap(object, GetNameNode.doSlowPath(object));
369365
}
@@ -391,7 +387,7 @@ public static Object doSlowPath(Object o) {
391387
} else if (o instanceof ManagedPythonClass) {
392388
return wrapNativeClass((ManagedPythonClass) o);
393389
} else if (o instanceof PythonAbstractObject) {
394-
assert !(o instanceof AbstractPythonClass);
390+
assert !PGuards.isClass(o);
395391
return PythonObjectNativeWrapper.wrapSlowPath((PythonAbstractObject) o);
396392
} else if (PGuards.isForeignObject(o)) {
397393
return TruffleObjectNativeWrapper.wrap((TruffleObject) o);
@@ -1616,6 +1612,9 @@ public static class GetTypeMemberNode extends GetNativeDictNode {
16161612
@Child private ToSulongNode toSulong;
16171613
@Child private AsPythonObjectNode toJava;
16181614
@Child private PCallCapiFunction callGetTpDictNode;
1615+
@Child private GetLazyClassNode getNativeClassNode;
1616+
1617+
@CompilationFinal private IsBuiltinClassProfile isTypeProfile;
16191618

16201619
private GetTypeMemberNode(String memberName) {
16211620
String getterFuncName = "get_" + memberName;
@@ -1632,9 +1631,19 @@ public Object execute(Object self) {
16321631
toSulong = insert(ToSulongNode.create());
16331632
toJava = insert(AsPythonObjectNode.create());
16341633
}
1634+
assert isNativeTypeObject(self);
16351635
return toJava.execute(callGetTpDictNode.call(toSulong.execute(self)));
16361636
}
16371637

1638+
private boolean isNativeTypeObject(Object self) {
1639+
if (getNativeClassNode == null || isTypeProfile == null) {
1640+
CompilerDirectives.transferToInterpreterAndInvalidate();
1641+
getNativeClassNode = insert(GetLazyClassNode.create());
1642+
isTypeProfile = IsBuiltinClassProfile.create();
1643+
}
1644+
return isTypeProfile.profileClass(getNativeClassNode.execute(self), PythonBuiltinClassType.PythonClass);
1645+
}
1646+
16381647
@TruffleBoundary
16391648
public static Object doSlowPath(Object self, String memberName) {
16401649
String getterFuncName = "get_" + memberName;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/superobject/SuperBuiltins.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.oracle.graal.python.builtins.objects.superobject.SuperBuiltinsFactory.GetTypeNodeGen;
6161
import com.oracle.graal.python.builtins.objects.superobject.SuperBuiltinsFactory.SuperInitNodeFactory;
6262
import com.oracle.graal.python.builtins.objects.type.AbstractPythonClass;
63+
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
6364
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroNode;
6465
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsSameTypeNode;
6566
import com.oracle.graal.python.nodes.SpecialAttributeNames;
@@ -158,6 +159,7 @@ public abstract static class SuperInitNode extends PythonVarargsBuiltinNode {
158159
@Child private GetClassNode getClassNode;
159160
@Child private LookupAndCallBinaryNode getAttrNode;
160161
@Child private CellBuiltins.GetRefNode getRefNode;
162+
@Child private TypeNodes.IsTypeNode isTypeNode;
161163

162164
@Override
163165
public Object varArgExecute(VirtualFrame frame, Object[] arguments, PKeyword[] keywords) throws VarargsBuiltinDirectInvocationNotSupported {
@@ -321,6 +323,14 @@ private GetClassNode getGetClass() {
321323
return getClassNode;
322324
}
323325

326+
private TypeNodes.IsTypeNode ensureIsTypeNode() {
327+
if (isTypeNode == null) {
328+
CompilerDirectives.transferToInterpreterAndInvalidate();
329+
isTypeNode = insert(TypeNodes.IsTypeNode.create());
330+
}
331+
return isTypeNode;
332+
}
333+
324334
private LookupAndCallBinaryNode getGetAttr() {
325335
if (getAttrNode == null) {
326336
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -345,7 +355,7 @@ private AbstractPythonClass supercheck(Object cls, Object object) {
345355
* not a subclass of type, but obj.__class__ is! This will allow using super() with a
346356
* proxy for obj.
347357
*/
348-
if (object instanceof AbstractPythonClass) {
358+
if (ensureIsTypeNode().execute(object)) {
349359
if (getIsSubtype().execute(object, cls)) {
350360
return (AbstractPythonClass) object;
351361
}
@@ -356,7 +366,7 @@ private AbstractPythonClass supercheck(Object cls, Object object) {
356366
} else {
357367
try {
358368
Object classObject = getGetAttr().executeObject(object, SpecialAttributeNames.__CLASS__);
359-
if (classObject instanceof AbstractPythonClass) {
369+
if (ensureIsTypeNode().execute(classObject)) {
360370
if (getIsSubtype().execute(classObject, cls)) {
361371
return (AbstractPythonClass) classObject;
362372
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeNodes.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@
7171
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetSuperClassNodeGen;
7272
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetTypeFlagsNodeGen;
7373
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.IsSameTypeNodeGen;
74+
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.IsTypeNodeGen;
7475
import com.oracle.graal.python.nodes.BuiltinNames;
7576
import com.oracle.graal.python.nodes.PGuards;
7677
import com.oracle.graal.python.nodes.PNodeWithContext;
7778
import com.oracle.graal.python.nodes.SpecialMethodNames;
79+
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
80+
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
7881
import com.oracle.graal.python.nodes.truffle.PythonTypes;
7982
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
8083
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
@@ -665,4 +668,35 @@ private static AbstractPythonClass[] mergeMROs(MROMergeState[] toMerge, List<Abs
665668

666669
}
667670

671+
public abstract static class IsTypeNode extends Node {
672+
673+
public abstract boolean execute(Object obj);
674+
675+
@Specialization
676+
boolean doManagedClass(@SuppressWarnings("unused") ManagedPythonClass obj) {
677+
return true;
678+
}
679+
680+
@Specialization
681+
boolean doBuiltinType(@SuppressWarnings("unused") PythonBuiltinClassType obj) {
682+
return true;
683+
}
684+
685+
@Specialization
686+
boolean doManagedClass(PythonAbstractNativeObject obj,
687+
@Cached("create()") IsBuiltinClassProfile profile,
688+
@Cached("create()") GetLazyClassNode getClassNode) {
689+
return profile.profileClass(getClassNode.execute(obj), PythonBuiltinClassType.PythonClass);
690+
}
691+
692+
@Fallback
693+
boolean doGeneric(@SuppressWarnings("unused") Object obj) {
694+
return false;
695+
}
696+
697+
public static IsTypeNode create() {
698+
return IsTypeNodeGen.create();
699+
}
700+
}
701+
668702
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/ExceptNode.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
3333
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroNode;
3434
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsSameTypeNode;
35+
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
3536
import com.oracle.graal.python.nodes.PNodeWithContext;
3637
import com.oracle.graal.python.nodes.expression.ExpressionNode;
3738
import com.oracle.graal.python.nodes.frame.WriteNode;
@@ -58,6 +59,7 @@ public class ExceptNode extends PNodeWithContext implements InstrumentableNode {
5859
@Child private GetLazyClassNode getClass;
5960
@Child private GetMroNode getMroNode;
6061
@Child private IsSameTypeNode isSameTypeNode;
62+
@Child private IsTypeNode isTypeNode;
6163

6264
// "object" is the uninitialized value (since it's not a valid error type)
6365
@CompilationFinal private PythonBuiltinClassType singleBuiltinError = PythonBuiltinClassType.PythonObject;
@@ -228,7 +230,7 @@ private boolean matches(Object expectedType, AbstractPythonClass clazz) {
228230
}
229231

230232
private boolean matches(Object expectedType, PythonBuiltinClassType clazz) {
231-
if (!(expectedType instanceof AbstractPythonClass)) {
233+
if (!isType(expectedType)) {
232234
errorProfile.enter();
233235
throw raiseNoException();
234236
}
@@ -296,4 +298,12 @@ private boolean isSameType(Object left, Object right) {
296298
}
297299
return isSameTypeNode.execute(left, right);
298300
}
301+
302+
private boolean isType(Object expectedType) {
303+
if (isTypeNode == null) {
304+
CompilerDirectives.transferToInterpreterAndInvalidate();
305+
isTypeNode = insert(IsTypeNode.create());
306+
}
307+
return isTypeNode.execute(expectedType);
308+
}
299309
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/interop/PythonMessageResolution.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
6969
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
7070
import com.oracle.graal.python.builtins.objects.type.PythonClass;
71+
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
7172
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroNode;
7273
import com.oracle.graal.python.nodes.PGuards;
7374
import com.oracle.graal.python.nodes.SpecialMethodNames;
@@ -586,8 +587,14 @@ public Object access(Object receiver) {
586587

587588
@Resolve(message = "IS_INSTANTIABLE")
588589
abstract static class IsInstantiableNode extends Node {
590+
@Child private TypeNodes.IsTypeNode isTypeNode;
591+
589592
public Object access(Object obj) {
590-
return obj instanceof LazyPythonClass;
593+
if (isTypeNode == null) {
594+
CompilerDirectives.transferToInterpreterAndInvalidate();
595+
isTypeNode = insert(TypeNodes.IsTypeNode.create());
596+
}
597+
return isTypeNode.execute(obj);
591598
}
592599
}
593600

0 commit comments

Comments
 (0)