Skip to content

Commit 04d63d0

Browse files
committed
add an empty call target for an empty codestring, similar to cpython
1 parent e114858 commit 04d63d0

File tree

1 file changed

+57
-46
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code

1 file changed

+57
-46
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
import com.oracle.graal.python.PythonLanguage;
5050
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
51+
import com.oracle.graal.python.builtins.objects.PNone;
5152
import com.oracle.graal.python.builtins.objects.cell.PCell;
5253
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
5354
import com.oracle.graal.python.builtins.objects.dict.PDict;
@@ -75,6 +76,7 @@
7576
import com.oracle.truffle.api.frame.FrameSlot;
7677
import com.oracle.truffle.api.frame.FrameSlotKind;
7778
import com.oracle.truffle.api.frame.MaterializedFrame;
79+
import com.oracle.truffle.api.frame.VirtualFrame;
7880
import com.oracle.truffle.api.nodes.Node;
7981
import com.oracle.truffle.api.nodes.NodeUtil;
8082
import com.oracle.truffle.api.nodes.RootNode;
@@ -158,60 +160,69 @@ public PCode(LazyPythonClass cls, int argcount, int kwonlyargcount,
158160

159161
// Derive a new call target from the code string, if we can
160162
RootNode rootNode = null;
161-
if ((flags & FLAG_MODULE) == 0) {
162-
// we're looking for the function, not the module
163-
String funcdef;
164-
if (freevars.length > 0) {
165-
// we build an outer function to provide the initial scoping
166-
String outernme = "_____" + System.nanoTime();
167-
StringBuilder sb = new StringBuilder();
168-
sb.append("def ").append(outernme).append("():\n");
169-
for (Object freevar : freevars) {
170-
String v;
171-
if (freevar instanceof PString) {
172-
v = ((PString) freevar).getValue();
173-
} else if (freevar instanceof String) {
174-
v = (String) freevar;
175-
} else {
176-
continue;
163+
if (codestring.length > 0) {
164+
if ((flags & FLAG_MODULE) == 0) {
165+
// we're looking for the function, not the module
166+
String funcdef;
167+
if (freevars.length > 0) {
168+
// we build an outer function to provide the initial scoping
169+
String outernme = "_____" + System.nanoTime();
170+
StringBuilder sb = new StringBuilder();
171+
sb.append("def ").append(outernme).append("():\n");
172+
for (Object freevar : freevars) {
173+
String v;
174+
if (freevar instanceof PString) {
175+
v = ((PString) freevar).getValue();
176+
} else if (freevar instanceof String) {
177+
v = (String) freevar;
178+
} else {
179+
continue;
180+
}
181+
sb.append(" ").append(v).append(" = None\n");
177182
}
178-
sb.append(" ").append(v).append(" = None\n");
183+
sb.append(" global ").append(name).append("\n");
184+
sb.append(" ").append(new String(codestring));
185+
sb.append("\n\n").append(outernme).append("()");
186+
funcdef = sb.toString();
187+
} else {
188+
funcdef = new String(codestring);
179189
}
180-
sb.append(" global ").append(name).append("\n");
181-
sb.append(" ").append(new String(codestring));
182-
sb.append("\n\n").append(outernme).append("()");
183-
funcdef = sb.toString();
184-
} else {
185-
funcdef = new String(codestring);
186-
}
187190

188-
rootNode = (RootNode) PythonLanguage.getCore().getParser().parse(ParserMode.File, PythonLanguage.getCore(), Source.newBuilder("python", funcdef, name).build(), null);
189-
Object[] args = PArguments.create();
190-
PDict globals = PythonLanguage.getCore().factory().createDict();
191-
PArguments.setGlobals(args, globals);
192-
Truffle.getRuntime().createCallTarget(rootNode).call(args);
193-
Object function = globals.getDictStorage().getItem(name, HashingStorage.getSlowPathEquivalence(name));
194-
if (function instanceof PFunction) {
195-
rootNode = ((PFunction) function).getFunctionRootNode();
191+
rootNode = (RootNode) PythonLanguage.getCore().getParser().parse(ParserMode.File, PythonLanguage.getCore(), Source.newBuilder("python", funcdef, name).build(), null);
192+
Object[] args = PArguments.create();
193+
PDict globals = PythonLanguage.getCore().factory().createDict();
194+
PArguments.setGlobals(args, globals);
195+
Truffle.getRuntime().createCallTarget(rootNode).call(args);
196+
Object function = globals.getDictStorage().getItem(name, HashingStorage.getSlowPathEquivalence(name));
197+
if (function instanceof PFunction) {
198+
rootNode = ((PFunction) function).getFunctionRootNode();
199+
} else {
200+
throw PythonLanguage.getCore().raise(PythonBuiltinClassType.ValueError, "got an invalid codestring trying to create a function code object");
201+
}
196202
} else {
197-
throw PythonLanguage.getCore().raise(PythonBuiltinClassType.ValueError, "got an invalid codestring trying to create a function code object");
203+
MaterializedFrame frame = null;
204+
if (freevars.length > 0) {
205+
FrameDescriptor frameDescriptor = new FrameDescriptor();
206+
frame = Truffle.getRuntime().createMaterializedFrame(new Object[0], frameDescriptor);
207+
for (int i = 0; i < freevars.length; i++) {
208+
Object ident = freevars[i];
209+
FrameSlot slot = frameDescriptor.addFrameSlot(ident);
210+
frameDescriptor.setFrameSlotKind(slot, FrameSlotKind.Object);
211+
frame.setObject(slot, new PCell());
212+
}
213+
}
214+
rootNode = (RootNode) PythonLanguage.getCore().getParser().parse(ParserMode.File, PythonLanguage.getCore(), Source.newBuilder("python", new String(codestring), name).build(), frame);
215+
assert rootNode instanceof ModuleRootNode;
198216
}
217+
this.callTarget = Truffle.getRuntime().createCallTarget(rootNode);
199218
} else {
200-
MaterializedFrame frame = null;
201-
if (freevars.length > 0) {
202-
FrameDescriptor frameDescriptor = new FrameDescriptor();
203-
frame = Truffle.getRuntime().createMaterializedFrame(new Object[0], frameDescriptor);
204-
for (int i = 0; i < freevars.length; i++) {
205-
Object ident = freevars[i];
206-
FrameSlot slot = frameDescriptor.addFrameSlot(ident);
207-
frameDescriptor.setFrameSlotKind(slot, FrameSlotKind.Object);
208-
frame.setObject(slot, new PCell());
219+
this.callTarget = Truffle.getRuntime().createCallTarget(new RootNode(PythonLanguage.getCurrent()) {
220+
@Override
221+
public Object execute(VirtualFrame frame) {
222+
return PNone.NONE;
209223
}
210-
}
211-
rootNode = (RootNode) PythonLanguage.getCore().getParser().parse(ParserMode.File, PythonLanguage.getCore(), Source.newBuilder("python", new String(codestring), name).build(), frame);
212-
assert rootNode instanceof ModuleRootNode;
224+
});
213225
}
214-
this.callTarget = Truffle.getRuntime().createCallTarget(rootNode);
215226

216227
char paramNom = 'A';
217228
String[] paramNames = new String[argcount];

0 commit comments

Comments
 (0)