Skip to content

Commit 6b0e6d7

Browse files
committed
[hotfix] pcode
PullRequest: graalpython/405
2 parents e199c09 + c5e9b2e commit 6b0e6d7

File tree

5 files changed

+91
-13
lines changed

5 files changed

+91
-13
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_code.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def test_firstlineno():
7777

7878

7979
def test_code_attributes():
80+
import sys
81+
8082
code = wrapper().__code__
8183
assert code.co_argcount == 3
8284
assert code.co_kwonlyargcount == 0
@@ -85,7 +87,8 @@ def test_code_attributes():
8587
assert code.co_flags & (1 << 5)
8688
assert not code.co_flags & (1 << 2)
8789
assert not code.co_flags & (1 << 3)
88-
# assert code.co_code
90+
if sys.implementation.name == 'graalpython':
91+
assert code.co_code.decode().strip() == wrapper().__truffle_source__.strip()
8992
# assert code.co_consts
9093
# assert set(code.co_names) == {'set', 'TypeError', 'print'}
9194
assert set(code.co_varnames) == {'arg_l', 'kwarg_case', 'kwarg_other', 'loc_1', 'loc_3', 'inner_func'}
@@ -133,3 +136,28 @@ def test_code_copy():
133136
assert code.co_lnotab == code2.co_lnotab
134137
assert set(code.co_freevars) == set(code2.co_freevars)
135138
assert set(code.co_cellvars) == set(code2.co_cellvars)
139+
140+
141+
def test_module_code():
142+
import sys
143+
m = __import__('package.moduleA')
144+
with open(m.__file__, 'r') as MODULE:
145+
source = MODULE.read()
146+
code = compile(source, m.__file__, 'exec')
147+
assert code.co_argcount == 0
148+
assert code.co_kwonlyargcount == 0
149+
assert code.co_nlocals == 0
150+
# assert code.co_stacksize == 0
151+
# assert code.co_flags == 0
152+
if sys.implementation.name == 'graalpython':
153+
assert code.co_code.decode().strip() == source.strip()
154+
# assert code.co_consts == tuple()
155+
# assert set(code.co_names) == set()
156+
assert set(code.co_varnames) == set()
157+
assert code.co_filename.endswith("__init__.py")
158+
assert code.co_name.startswith("<module")
159+
if sys.implementation.name == 'graalpython':
160+
assert code.co_firstlineno == 1
161+
# assert code.co_lnotab == b''
162+
assert code.co_freevars == tuple()
163+
assert code.co_cellvars == tuple()

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
import com.oracle.truffle.api.CompilerDirectives;
172172
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
173173
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
174+
import com.oracle.truffle.api.RootCallTarget;
174175
import com.oracle.truffle.api.Truffle;
175176
import com.oracle.truffle.api.debug.Debugger;
176177
import com.oracle.truffle.api.dsl.Cached;
@@ -681,7 +682,24 @@ Object execCustomGlobalsGlobalLocals(Object source, PDict globals, @SuppressWarn
681682
// fall back (like their CPython counterparts) to writing to the globals. We only need
682683
// to ensure that the `locals()` call still gives us the globals dict
683684
PArguments.setPFrame(args, factory().createPFrame(globals));
684-
return indirectCallNode.call(code.getRootCallTarget(), args);
685+
RootCallTarget rootCallTarget = code.getRootCallTarget();
686+
if (rootCallTarget == null) {
687+
rootCallTarget = extractRootCallTargetFromSource(code);
688+
}
689+
if (rootCallTarget == null) {
690+
throw raise(ValueError, "cannot create the rootCallTarget from the code object: %p", code);
691+
}
692+
return indirectCallNode.call(rootCallTarget, args);
693+
}
694+
695+
@TruffleBoundary
696+
private RootCallTarget extractRootCallTargetFromSource(PCode code) {
697+
Source src = Source.newBuilder("python", new String(code.getCodestring()), code.getName()).build();
698+
Node root = getCore().getParser().parse(ParserMode.File, getCore(), src, null);
699+
if (root instanceof RootNode) {
700+
return Truffle.getRuntime().createCallTarget((RootNode) root);
701+
}
702+
return null;
685703
}
686704

687705
@Specialization(guards = {"isMapping(locals)"})

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -58,8 +58,8 @@
5858
import com.oracle.graal.python.nodes.function.FunctionRootNode;
5959
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
6060
import com.oracle.graal.python.runtime.PythonCore;
61-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6261
import com.oracle.truffle.api.CompilerDirectives;
62+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6363
import com.oracle.truffle.api.RootCallTarget;
6464
import com.oracle.truffle.api.Truffle;
6565
import com.oracle.truffle.api.nodes.Node;
@@ -158,6 +158,8 @@ private static String[] extractFreeVars(RootNode rootNode) {
158158
return ((FunctionRootNode) rootNode).getFreeVars();
159159
} else if (rootNode instanceof GeneratorFunctionRootNode) {
160160
return ((GeneratorFunctionRootNode) rootNode).getFreeVars();
161+
} else if (rootNode instanceof ModuleRootNode) {
162+
return ((ModuleRootNode) rootNode).getFreeVars();
161163
} else {
162164
return null;
163165
}
@@ -168,6 +170,8 @@ private static String[] extractCellVars(RootNode rootNode) {
168170
return ((FunctionRootNode) rootNode).getCellVars();
169171
} else if (rootNode instanceof GeneratorFunctionRootNode) {
170172
return ((GeneratorFunctionRootNode) rootNode).getCellVars();
173+
} else if (rootNode instanceof ModuleRootNode) {
174+
return new String[0];
171175
} else {
172176
return null;
173177
}
@@ -189,13 +193,16 @@ private static String extractFileName(RootNode rootNode) {
189193
private static int extractFirstLineno(RootNode rootNode) {
190194
RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode) ? ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode() : rootNode;
191195
SourceSection sourceSection = funcRootNode.getSourceSection();
192-
return (sourceSection != null) ? sourceSection.getStartLine() : 1;
196+
if (sourceSection != null) {
197+
return sourceSection.getStartLine();
198+
}
199+
return 1;
193200
}
194201

195202
private static String extractName(RootNode rootNode) {
196203
String name;
197204
if (rootNode instanceof ModuleRootNode) {
198-
name = "<module>";
205+
name = rootNode.getName();
199206
} else if (rootNode instanceof FunctionRootNode) {
200207
name = ((FunctionRootNode) rootNode).getFunctionName();
201208
} else {
@@ -299,6 +306,19 @@ private void extractArgStats() {
299306
this.nlocals = varnamesSet.size();
300307
}
301308

309+
@TruffleBoundary
310+
private static byte[] extractCodeString(RootNode rootNode) {
311+
RootNode funcRootNode = rootNode;
312+
if (rootNode instanceof GeneratorFunctionRootNode) {
313+
funcRootNode = ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode();
314+
}
315+
SourceSection sourceSection = funcRootNode.getSourceSection();
316+
if (sourceSection != null) {
317+
return sourceSection.getCharacters().toString().getBytes();
318+
}
319+
return null;
320+
}
321+
302322
public RootNode getRootNode() {
303323
return rootNode;
304324
}
@@ -389,6 +409,9 @@ public Object[] getVarnames() {
389409
}
390410

391411
public byte[] getCodestring() {
412+
if (codestring == null && hasRootNode()) {
413+
this.codestring = extractCodeString(getRootNode());
414+
}
392415
return codestring;
393416
}
394417

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PClosureRootNode.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -90,10 +90,13 @@ public boolean hasFreeVars() {
9090
}
9191

9292
public String[] getFreeVars() {
93-
String[] freeVars = new String[freeVarSlots.length];
94-
for (int i = 0; i < freeVarSlots.length; i++) {
95-
freeVars[i] = (String) freeVarSlots[i].getIdentifier();
93+
if (freeVarSlots != null) {
94+
String[] freeVars = new String[freeVarSlots.length];
95+
for (int i = 0; i < freeVarSlots.length; i++) {
96+
freeVars[i] = (String) freeVarSlots[i].getIdentifier();
97+
}
98+
return freeVars;
9699
}
97-
return freeVars;
100+
return new String[0];
98101
}
99102
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/ExpressionNode.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -53,6 +53,8 @@
5353
import com.oracle.truffle.api.nodes.Node;
5454
import com.oracle.truffle.api.nodes.NodeCost;
5555
import com.oracle.truffle.api.nodes.UnexpectedResultException;
56+
import com.oracle.truffle.api.source.Source;
57+
import com.oracle.truffle.api.source.SourceSection;
5658

5759
/**
5860
* Base class for all expressions. Expressions always return a value.
@@ -176,7 +178,11 @@ public static final class ExpressionWithSideEffects extends ExpressionNode {
176178
private ExpressionWithSideEffects(ExpressionNode node, StatementNode[] sideEffects) {
177179
this.node = node;
178180
this.sideEffects = sideEffects;
179-
this.assignSourceSection(node.getSourceSection());
181+
SourceSection sourceSection = node.getSourceSection();
182+
if (sourceSection != null) {
183+
Source source = sourceSection.getSource();
184+
this.assignSourceSection(source.createSection(0, source.getLength()));
185+
}
180186
}
181187

182188
@Override

0 commit comments

Comments
 (0)