Skip to content

Commit 3005b36

Browse files
committed
move the super object into Java
1 parent 673f85d commit 3005b36

File tree

10 files changed

+479
-195
lines changed

10 files changed

+479
-195
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Builtin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757

5858
boolean isPublic() default true;
5959

60+
/**
61+
* By default the caller frame bit is set on-demand, but for some builtins it might be useful to
62+
* always force passing the caller frame.
63+
*/
64+
boolean alwaysNeedsCallerFrame() default false;
65+
6066
/**
6167
* Module functions should be bound to their module, meaning they would take the module itself
6268
* as "self" parameter. We omit this by default, but if the builtin does explicitly specify the

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
import com.oracle.graal.python.builtins.objects.set.SetBuiltins;
125125
import com.oracle.graal.python.builtins.objects.slice.SliceBuiltins;
126126
import com.oracle.graal.python.builtins.objects.str.StringBuiltins;
127+
import com.oracle.graal.python.builtins.objects.superobject.SuperBuiltins;
127128
import com.oracle.graal.python.builtins.objects.traceback.TracebackBuiltins;
128129
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins;
129130
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
@@ -265,6 +266,7 @@ public final class Python3Core implements PythonCore {
265266
new SysModuleBuiltins(),
266267
new BufferBuiltins(),
267268
new MemoryviewBuiltins(),
269+
new SuperBuiltins(),
268270
};
269271

270272
// not using EnumMap, HashMap, etc. to allow this to fold away during partial evaluation

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public enum PythonBuiltinClassType {
8989
PythonModule(com.oracle.graal.python.builtins.objects.module.PythonModule.class, "module"),
9090
PythonObject(com.oracle.graal.python.builtins.objects.object.PythonObject.class, "object"),
9191
PythonNativeObject(com.oracle.graal.python.builtins.objects.cext.PythonNativeObject.class, "object"),
92+
Super(com.oracle.graal.python.builtins.objects.superobject.SuperObject.class, "super"),
9293
PCode(com.oracle.graal.python.builtins.objects.code.PCode.class, "code"),
9394
PZip(com.oracle.graal.python.builtins.objects.iterator.PZip.class, "zip"),
9495
PBuffer(com.oracle.graal.python.builtins.objects.memoryview.PBuffer.class, "buffer");

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import static com.oracle.graal.python.nodes.BuiltinNames.REVERSED;
4444
import static com.oracle.graal.python.nodes.BuiltinNames.SET;
4545
import static com.oracle.graal.python.nodes.BuiltinNames.STR;
46+
import static com.oracle.graal.python.nodes.BuiltinNames.SUPER;
4647
import static com.oracle.graal.python.nodes.BuiltinNames.TUPLE;
4748
import static com.oracle.graal.python.nodes.BuiltinNames.TYPE;
4849
import static com.oracle.graal.python.nodes.BuiltinNames.ZIP;
@@ -119,6 +120,7 @@
119120
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
120121
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
121122
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
123+
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
122124
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
123125
import com.oracle.graal.python.nodes.object.GetClassNode;
124126
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
@@ -2070,4 +2072,13 @@ public PMemoryView doGeneric(PythonClass cls, Object value) {
20702072
}
20712073
}
20722074

2075+
// super()
2076+
@Builtin(name = SUPER, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 3, constructsClass = PythonBuiltinClassType.Super)
2077+
@GenerateNodeFactory
2078+
public abstract static class SuperInitNode extends PythonTernaryBuiltinNode {
2079+
@Specialization
2080+
Object doObjectIndirect(PythonClass self, @SuppressWarnings("unused") Object type, @SuppressWarnings("unused") Object object) {
2081+
return factory().createSuperObject(self);
2082+
}
2083+
}
20732084
}

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

Lines changed: 0 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43-
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__CLASS__;
4443
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
4544

4645
import java.io.IOException;
@@ -54,21 +53,11 @@
5453
import com.oracle.graal.python.builtins.CoreFunctions;
5554
import com.oracle.graal.python.builtins.PythonBuiltins;
5655
import com.oracle.graal.python.builtins.objects.PNone;
57-
import com.oracle.graal.python.builtins.objects.cell.PCell;
5856
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
59-
import com.oracle.graal.python.builtins.objects.function.PArguments;
60-
import com.oracle.graal.python.builtins.objects.function.PKeyword;
6157
import com.oracle.graal.python.builtins.objects.ints.PInt;
6258
import com.oracle.graal.python.builtins.objects.str.PString;
63-
import com.oracle.graal.python.builtins.objects.type.PythonClass;
64-
import com.oracle.graal.python.nodes.argument.ReadIndexedArgumentNode;
65-
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
66-
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
67-
import com.oracle.graal.python.nodes.frame.ReadLocalVariableNode;
68-
import com.oracle.graal.python.nodes.function.BuiltinFunctionRootNode;
6959
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7060
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
71-
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
7261
import com.oracle.graal.python.runtime.PythonContext;
7362
import com.oracle.graal.python.runtime.PythonCore;
7463
import com.oracle.graal.python.runtime.PythonOptions;
@@ -79,18 +68,12 @@
7968
import com.oracle.truffle.api.Truffle;
8069
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
8170
import com.oracle.truffle.api.TruffleOptions;
82-
import com.oracle.truffle.api.dsl.Cached;
83-
import com.oracle.truffle.api.dsl.Fallback;
8471
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
8572
import com.oracle.truffle.api.dsl.NodeFactory;
8673
import com.oracle.truffle.api.dsl.Specialization;
87-
import com.oracle.truffle.api.frame.Frame;
88-
import com.oracle.truffle.api.frame.FrameSlot;
89-
import com.oracle.truffle.api.frame.FrameSlotTypeException;
9074
import com.oracle.truffle.api.frame.VirtualFrame;
9175
import com.oracle.truffle.api.nodes.DirectCallNode;
9276
import com.oracle.truffle.api.nodes.RootNode;
93-
import com.oracle.truffle.api.profiles.ConditionProfile;
9477

9578
@CoreFunctions(defineModule = "sys")
9679
public class SysModuleBuiltins extends PythonBuiltins {
@@ -200,143 +183,6 @@ public void initialize(PythonCore core) {
200183
super.initialize(core);
201184
}
202185

203-
@Builtin(name = "__super__init__", minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true)
204-
@GenerateNodeFactory
205-
public abstract static class SuperInitNode extends PythonVarargsBuiltinNode {
206-
207-
@Child private SetAttributeNode setTypeAttribute = SetAttributeNode.create("__type__");
208-
@Child private SetAttributeNode setObjAttribute = SetAttributeNode.create("__obj__");
209-
210-
@Override
211-
public Object varArgExecute(VirtualFrame frame, Object[] arguments, PKeyword[] keywords) throws VarargsBuiltinDirectInvocationNotSupported {
212-
if (keywords.length != 0) {
213-
throw raise(PythonErrorType.RuntimeError, "super(): unexpected keyword arguments");
214-
}
215-
if (arguments.length == 1) {
216-
return execute(frame, arguments[0], PNone.NO_VALUE, PNone.NO_VALUE);
217-
} else if (arguments.length == 2) {
218-
return execute(frame, arguments[0], arguments[1], PNone.NO_VALUE);
219-
} else if (arguments.length == 3) {
220-
return execute(frame, arguments[0], arguments[1], arguments[2]);
221-
} else {
222-
throw raise(PythonErrorType.RuntimeError, "super(): invalid number of arguments");
223-
}
224-
}
225-
226-
@Override
227-
public final Object execute(VirtualFrame frame, Object self, Object[] arguments, PKeyword[] keywords) {
228-
if (keywords.length != 0) {
229-
throw raise(PythonErrorType.RuntimeError, "super(): unexpected keyword arguments");
230-
}
231-
if (arguments.length == 0) {
232-
return execute(frame, self, PNone.NO_VALUE, PNone.NO_VALUE);
233-
} else if (arguments.length == 1) {
234-
return execute(frame, self, arguments[0], PNone.NO_VALUE);
235-
} else if (arguments.length == 2) {
236-
return execute(frame, self, arguments[0], arguments[1]);
237-
} else {
238-
throw raise(PythonErrorType.RuntimeError, "super(): too many arguments");
239-
}
240-
}
241-
242-
protected abstract Object execute(VirtualFrame frame, Object self, Object cls, Object obj);
243-
244-
@Specialization(guards = {"!isNoValue(cls)", "!isNoValue(obj)"})
245-
PNone init(Object self, Object cls, Object obj) {
246-
if (!(cls instanceof PythonClass)) {
247-
throw raise(PythonErrorType.RuntimeError, "super(): __class__ is not a type (%p)", cls);
248-
}
249-
setTypeAttribute.executeVoid(self, cls);
250-
setObjAttribute.executeVoid(self, obj);
251-
return PNone.NONE;
252-
}
253-
254-
protected boolean isInBuiltinFunctionRoot() {
255-
return getRootNode() instanceof BuiltinFunctionRootNode;
256-
}
257-
258-
protected ReadLocalVariableNode createRead(VirtualFrame frame) {
259-
FrameSlot slot = frame.getFrameDescriptor().findFrameSlot(__CLASS__);
260-
if (slot == null) {
261-
throw raise(PythonErrorType.RuntimeError, "super(): empty __class__ cell");
262-
}
263-
return ReadLocalVariableNode.create(slot);
264-
}
265-
266-
/**
267-
* Executed with the frame of the calling method - direct access to the frame.
268-
*/
269-
@Specialization(guards = {"!isInBuiltinFunctionRoot()", "isNoValue(clsArg)", "isNoValue(objArg)"})
270-
PNone initInPlace(VirtualFrame frame, Object self, @SuppressWarnings("unused") PNone clsArg, @SuppressWarnings("unused") PNone objArg,
271-
@Cached("createRead(frame)") ReadLocalVariableNode readClass,
272-
@Cached("create(0)") ReadIndexedArgumentNode readArgument,
273-
@Cached("createBinaryProfile()") ConditionProfile isCellProfile) {
274-
Object obj = readArgument.execute(frame);
275-
if (obj == PNone.NONE) {
276-
throw raise(PythonErrorType.RuntimeError, "super(): no arguments");
277-
}
278-
Object cls = readClass.execute(frame);
279-
if (isCellProfile.profile(cls instanceof PCell)) {
280-
cls = ((PCell) cls).getPythonRef();
281-
}
282-
if (cls == PNone.NONE) {
283-
throw raise(PythonErrorType.RuntimeError, "super(): empty __class__ cell");
284-
}
285-
return init(self, cls, obj);
286-
}
287-
288-
/**
289-
* Executed within a {@link BuiltinFunctionRootNode} - indirect access to the frame.
290-
*/
291-
@Specialization(guards = {"isInBuiltinFunctionRoot()", "isNoValue(clsArg)", "isNoValue(objArg)"})
292-
PNone init(VirtualFrame frame, Object self, @SuppressWarnings("unused") PNone clsArg, @SuppressWarnings("unused") PNone objArg,
293-
@Cached("create(0)") ReadCallerFrameNode readCaller) {
294-
Frame target = readCaller.executeWith(frame);
295-
if (target == null) {
296-
throw raise(PythonErrorType.RuntimeError, "super(): no current frame");
297-
}
298-
Object[] arguments = target.getArguments();
299-
if (arguments.length <= PArguments.USER_ARGUMENTS_OFFSET) {
300-
throw raise(PythonErrorType.RuntimeError, "super(): no arguments");
301-
}
302-
Object obj = arguments[PArguments.USER_ARGUMENTS_OFFSET];
303-
if (obj == PNone.NONE) {
304-
throw raise(PythonErrorType.RuntimeError, "super(): no arguments");
305-
}
306-
307-
return initFromFrame(self, target, obj);
308-
}
309-
310-
@TruffleBoundary
311-
private PNone initFromFrame(Object self, Frame target, Object obj) {
312-
// TODO: remove me
313-
// TODO: do it properly via the python API in super.__init__ :
314-
// sys._getframe(1).f_code.co_closure?
315-
FrameSlot classSlot = target.getFrameDescriptor().findFrameSlot(__CLASS__);
316-
Object cls = PNone.NONE;
317-
if (classSlot != null) {
318-
try {
319-
cls = target.getObject(classSlot);
320-
if (cls instanceof PCell) {
321-
cls = ((PCell) cls).getPythonRef();
322-
}
323-
} catch (FrameSlotTypeException e) {
324-
// fallthrough
325-
}
326-
}
327-
if (cls == PNone.NONE) {
328-
throw raise(PythonErrorType.RuntimeError, "super(): empty __class__ cell");
329-
}
330-
return init(self, cls, obj);
331-
}
332-
333-
@SuppressWarnings("unused")
334-
@Fallback
335-
PNone initFallback(Object self, Object cls, Object obj) {
336-
throw raise(PythonErrorType.RuntimeError, "super(): invalid arguments");
337-
}
338-
}
339-
340186
@Builtin(name = "exc_info", fixedNumOfPositionalArgs = 0)
341187
@GenerateNodeFactory
342188
public static abstract class ExcInfoNode extends PythonBuiltinNode {

0 commit comments

Comments
 (0)