Skip to content

Commit 1722a2f

Browse files
committed
Fix calling __new__ when it's not a descriptor
1 parent e2d4a19 commit 1722a2f

File tree

1 file changed

+34
-15
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type

1 file changed

+34
-15
lines changed

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import com.oracle.graal.python.nodes.BuiltinNames;
102102
import com.oracle.graal.python.nodes.ErrorMessages;
103103
import com.oracle.graal.python.nodes.PGuards;
104+
import com.oracle.graal.python.nodes.PNodeWithContext;
104105
import com.oracle.graal.python.nodes.PNodeWithRaise;
105106
import com.oracle.graal.python.nodes.SpecialAttributeNames;
106107
import com.oracle.graal.python.nodes.argument.positional.PositionalArgumentsNode;
@@ -113,7 +114,6 @@
113114
import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
114115
import com.oracle.graal.python.nodes.call.special.CallVarargsMethodNode;
115116
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
116-
import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode;
117117
import com.oracle.graal.python.nodes.call.special.LookupSpecialMethodNode;
118118
import com.oracle.graal.python.nodes.classes.AbstractObjectGetBasesNode;
119119
import com.oracle.graal.python.nodes.classes.AbstractObjectIsSubclassNode;
@@ -363,11 +363,42 @@ Object selfSeparate(VirtualFrame frame, Object self, Object[] arguments, PKeywor
363363
}
364364
}
365365

366+
@ReportPolymorphism
367+
protected abstract static class BindNew extends PNodeWithContext {
368+
public abstract Object execute(VirtualFrame frame, Object descriptor, Object type);
369+
370+
@Specialization
371+
static Object doBuiltin(PBuiltinFunction descriptor, @SuppressWarnings("unused") Object type) {
372+
return descriptor;
373+
}
374+
375+
@Specialization
376+
static Object doFunction(PFunction descriptor, @SuppressWarnings("unused") Object type) {
377+
return descriptor;
378+
}
379+
380+
@Fallback
381+
static Object doBind(VirtualFrame frame, Object descriptor, Object type,
382+
@Cached GetClassNode getClassNode,
383+
@Cached(parameters = "Get") LookupCallableSlotInMRONode lookupGet,
384+
@Cached CallTernaryMethodNode callGet) {
385+
Object getMethod = lookupGet.execute(getClassNode.execute(descriptor));
386+
if (getMethod != PNone.NO_VALUE) {
387+
return callGet.execute(frame, getMethod, descriptor, PNone.NONE, type);
388+
}
389+
return descriptor;
390+
}
391+
392+
public static BindNew create() {
393+
return TypeBuiltinsFactory.BindNewNodeGen.create();
394+
}
395+
}
396+
366397
@ReportPolymorphism
367398
protected abstract static class CallNodeHelper extends PNodeWithRaise {
368399
@Child private CallVarargsMethodNode dispatchNew = CallVarargsMethodNode.create();
369-
@Child private LookupAndCallTernaryNode callNewGet = LookupAndCallTernaryNode.create(__GET__);
370400
@Child private LookupAttributeInMRONode lookupNew = LookupAttributeInMRONode.create(__NEW__);
401+
@Child private BindNew bindNew = BindNew.create();
371402
@Child private CallVarargsMethodNode dispatchInit;
372403
@Child private LookupSpecialMethodNode lookupInit;
373404
@Child private IsSubtypeNode isSubTypeNode;
@@ -378,8 +409,6 @@ protected abstract static class CallNodeHelper extends PNodeWithRaise {
378409
@CompilationFinal private ConditionProfile hasInit = ConditionProfile.createBinaryProfile();
379410
@CompilationFinal private ConditionProfile gotInitResult = ConditionProfile.createBinaryProfile();
380411

381-
@CompilationFinal private boolean newWasDescriptor = false;
382-
383412
abstract Object execute(VirtualFrame frame, Object self, Object[] args, PKeyword[] keywords, boolean doCreateArgs);
384413

385414
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()", guards = {"self == cachedSelf"}, assumptions = "singleContextAssumption()")
@@ -450,17 +479,7 @@ private Object op(VirtualFrame frame, Object self, Object[] arguments, PKeyword[
450479
if (hasNew.profile(newMethod != PNone.NO_VALUE)) {
451480
CompilerAsserts.partialEvaluationConstant(doCreateArgs);
452481
Object[] newArgs = doCreateArgs ? PositionalArgumentsNode.prependArgument(self, arguments) : arguments;
453-
Object newInstance;
454-
if (!newWasDescriptor && (newMethod instanceof PFunction || newMethod instanceof PBuiltinFunction)) {
455-
newInstance = dispatchNew.execute(frame, newMethod, newArgs, keywords);
456-
} else {
457-
if (!newWasDescriptor) {
458-
CompilerDirectives.transferToInterpreterAndInvalidate();
459-
reportPolymorphicSpecialize();
460-
newWasDescriptor = true;
461-
}
462-
newInstance = dispatchNew.execute(frame, callNewGet.execute(frame, newMethod, PNone.NONE, self), newArgs, keywords);
463-
}
482+
Object newInstance = dispatchNew.execute(frame, bindNew.execute(frame, newMethod, self), newArgs, keywords);
464483

465484
// see typeobject.c#type_call()
466485
// Ugly exception: when the call was type(something),

0 commit comments

Comments
 (0)