Skip to content

Commit 9ebe8bd

Browse files
committed
Set name and filename for empty code object's root node
Fixes #167
1 parent 77286d1 commit 9ebe8bd

File tree

5 files changed

+97
-21
lines changed

5 files changed

+97
-21
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import com.oracle.graal.python.nodes.IndirectCallNode;
4848
import com.oracle.graal.python.nodes.PNodeWithContext;
4949
import com.oracle.graal.python.nodes.PRootNode;
50+
import com.oracle.graal.python.nodes.PRootNodeWithFileName;
51+
import com.oracle.graal.python.nodes.util.BadOPCodeNode;
5052
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
5153
import com.oracle.graal.python.runtime.PythonContext;
5254
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
@@ -114,6 +116,12 @@ private PCode createCode(PythonContext context, Object cls, @SuppressWarnings("u
114116

115117
Supplier<CallTarget> createCode = () -> {
116118
RootNode rootNode = context.getCore().getSerializer().deserialize(getEmptySource(), codedata, toStringArray(cellvars), toStringArray(freevars));
119+
if (rootNode instanceof BadOPCodeNode) {
120+
((BadOPCodeNode) rootNode).setName(name);
121+
}
122+
if (rootNode instanceof PRootNodeWithFileName) {
123+
((PRootNodeWithFileName) rootNode).setFileName(filename);
124+
}
117125
return PythonUtils.getOrCreateCallTarget(rootNode);
118126
};
119127

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.oracle.graal.python.nodes.PClosureFunctionRootNode;
5757
import com.oracle.graal.python.nodes.PClosureRootNode;
5858
import com.oracle.graal.python.nodes.PRootNode;
59+
import com.oracle.graal.python.nodes.PRootNodeWithFileName;
5960
import com.oracle.graal.python.nodes.argument.ReadVarArgsNode;
6061
import com.oracle.graal.python.nodes.argument.ReadVarKeywordsNode;
6162
import com.oracle.graal.python.nodes.frame.FrameSlotIDs;
@@ -190,8 +191,8 @@ private static String[] extractCellVars(RootNode rootNode) {
190191
@TruffleBoundary
191192
private static void setRootNodeFileName(RootNode rootNode, String filename) {
192193
RootNode funcRootNode = rootNodeForExtraction(rootNode);
193-
if (funcRootNode instanceof PClosureRootNode) {
194-
((PClosureRootNode) funcRootNode).setFileName(filename);
194+
if (funcRootNode instanceof PRootNodeWithFileName) {
195+
((PRootNodeWithFileName) funcRootNode).setFileName(filename);
195196
}
196197
}
197198

@@ -200,15 +201,15 @@ public static String extractFileName(RootNode rootNode) {
200201
RootNode funcRootNode = rootNodeForExtraction(rootNode);
201202
SourceSection src = funcRootNode.getSourceSection();
202203

203-
if (funcRootNode instanceof PClosureRootNode) {
204-
PClosureRootNode closureRootNode = (PClosureRootNode) funcRootNode;
205-
if (closureRootNode.getFileName() != null) {
204+
if (funcRootNode instanceof PRootNodeWithFileName) {
205+
PRootNodeWithFileName rootNodeWithFileName = (PRootNodeWithFileName) funcRootNode;
206+
if (rootNodeWithFileName.getFileName() != null) {
206207
// for compiled modules, _imp._fix_co_filename will set the filename
207-
return closureRootNode.getFileName();
208+
return rootNodeWithFileName.getFileName();
208209
} else if (src != null) {
209210
return getSourceSectionFileName(src);
210211
} else {
211-
return closureRootNode.getName();
212+
return rootNodeWithFileName.getName();
212213
}
213214
} else if (src != null) {
214215
return getSourceSectionFileName(src);

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@
5252
import com.oracle.truffle.api.frame.VirtualFrame;
5353
import com.oracle.truffle.api.nodes.ExplodeLoop;
5454

55-
public abstract class PClosureRootNode extends PRootNode {
56-
private String fileName;
55+
public abstract class PClosureRootNode extends PRootNodeWithFileName {
5756
private final Assumption singleContextAssumption;
5857
private final boolean annotationsAvailable;
5958
@CompilerDirectives.CompilationFinal(dimensions = 1) protected final FrameSlot[] freeVarSlots;
@@ -129,12 +128,4 @@ public String[] getFreeVars() {
129128
public boolean hasAnnotations() {
130129
return annotationsAvailable;
131130
}
132-
133-
public void setFileName(String fileName) {
134-
this.fileName = fileName;
135-
}
136-
137-
public String getFileName() {
138-
return fileName;
139-
}
140131
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.nodes;
42+
43+
import com.oracle.truffle.api.TruffleLanguage;
44+
import com.oracle.truffle.api.frame.FrameDescriptor;
45+
46+
public abstract class PRootNodeWithFileName extends PRootNode {
47+
private String fileName;
48+
49+
public PRootNodeWithFileName(TruffleLanguage<?> language) {
50+
super(language);
51+
}
52+
53+
public PRootNodeWithFileName(TruffleLanguage<?> language, FrameDescriptor frameDescriptor) {
54+
super(language, frameDescriptor);
55+
}
56+
57+
public void setFileName(String fileName) {
58+
this.fileName = fileName;
59+
}
60+
61+
public String getFileName() {
62+
return fileName;
63+
}
64+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/BadOPCodeNode.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,18 @@
4444
import com.oracle.graal.python.PythonLanguage;
4545
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4646
import com.oracle.graal.python.builtins.objects.function.Signature;
47-
import com.oracle.graal.python.nodes.PRootNode;
47+
import com.oracle.graal.python.nodes.PRootNodeWithFileName;
4848
import com.oracle.graal.python.runtime.PythonContext;
4949
import com.oracle.truffle.api.CompilerDirectives;
50+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
5051
import com.oracle.truffle.api.TruffleLanguage;
5152
import com.oracle.truffle.api.frame.VirtualFrame;
5253

53-
public class BadOPCodeNode extends PRootNode {
54+
public class BadOPCodeNode extends PRootNodeWithFileName {
5455

55-
@CompilerDirectives.CompilationFinal private TruffleLanguage.ContextReference<PythonContext> context;
56+
private String name = "<invalid code>";
57+
58+
@CompilationFinal private TruffleLanguage.ContextReference<PythonContext> context;
5659

5760
public BadOPCodeNode(TruffleLanguage<?> language) {
5861
super(language);
@@ -78,6 +81,15 @@ public Signature getSignature() {
7881

7982
@Override
8083
public boolean isPythonInternal() {
81-
return true;
84+
return false;
85+
}
86+
87+
@Override
88+
public String getName() {
89+
return name;
90+
}
91+
92+
public void setName(String name) {
93+
this.name = name;
8294
}
8395
}

0 commit comments

Comments
 (0)