Skip to content

Commit 8042823

Browse files
committed
ImportStarNode: write submodules to locals not globals
1 parent 25463d8 commit 8042823

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/ReadLocalsNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,8 @@ Object frameToUpdate(PFrame frame,
9797
@Shared("factory") @Cached PythonObjectFactory factory) {
9898
return frame.getLocals(factory);
9999
}
100+
101+
public static ReadLocalsNode create() {
102+
return ReadLocalsNodeGen.create();
103+
}
100104
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/ImportStarNode.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
2929
import com.oracle.graal.python.builtins.objects.dict.PDict;
30+
import com.oracle.graal.python.builtins.objects.frame.PFrame;
3031
import com.oracle.graal.python.builtins.objects.function.PArguments;
3132
import com.oracle.graal.python.builtins.objects.mappingproxy.PMappingproxy;
3233
import com.oracle.graal.python.builtins.objects.module.PythonModule;
@@ -38,6 +39,8 @@
3839
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
3940
import com.oracle.graal.python.nodes.attributes.GetAttributeNode.GetAnyAttributeNode;
4041
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
42+
import com.oracle.graal.python.nodes.frame.MaterializeFrameNode;
43+
import com.oracle.graal.python.nodes.frame.ReadLocalsNode;
4144
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
4245
import com.oracle.graal.python.nodes.subscript.GetItemNode;
4346
import com.oracle.graal.python.nodes.subscript.SetItemNode;
@@ -65,6 +68,8 @@ public class ImportStarNode extends AbstractImportNode {
6568
@Child private CastToJavaStringNode castToStringNode;
6669
@Child private GetAnyAttributeNode readNode;
6770
@Child private PRaiseNode raiseNode;
71+
@Child private MaterializeFrameNode getPFrameNode;
72+
@Child private ReadLocalsNode getLocalsNode;
6873

6974
@Child private IsBuiltinClassProfile isAttributeErrorProfile;
7075

@@ -105,7 +110,8 @@ public ImportStarNode(String moduleName, int level) {
105110
@Override
106111
public void executeVoid(VirtualFrame frame) {
107112
Object importedModule = importModule(frame, moduleName, PArguments.getGlobals(frame), new String[]{"*"}, level);
108-
PythonObject globals = PArguments.getGlobals(frame);
113+
PFrame pFrame = ensureGetPFrameNode().execute(frame, this, true, true);
114+
PythonObject locals = (PythonObject) ensureGetLocalsNode().execute(frame, pFrame);
109115

110116
if (javaImport.profile(emulateJython() && getContext().getEnv().isHostObject(importedModule))) {
111117
try {
@@ -116,7 +122,7 @@ public void executeVoid(VirtualFrame frame) {
116122
// interop protocol guarantees these are Strings
117123
String attrName = (String) interopLib.readArrayElement(hostAttrs, i);
118124
Object attr = interopLib.readMember(importedModule, attrName);
119-
writeAttribute(frame, globals, attrName, attr);
125+
writeAttribute(frame, locals, attrName, attr);
120126
}
121127
} catch (UnknownIdentifierException | UnsupportedMessageException | InvalidArrayIndexException e) {
122128
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -137,7 +143,7 @@ public void executeVoid(VirtualFrame frame) {
137143
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.ATTR_NAME_MUST_BE_STRING, attrNameObj);
138144
}
139145
Object attr = readAttribute(frame, importedModule, attrName);
140-
writeAttribute(frame, globals, attrName, attr);
146+
writeAttribute(frame, locals, attrName, attr);
141147
}
142148
} catch (PException e) {
143149
e.expectAttributeError(ensureIsAttributeErrorProfile());
@@ -148,7 +154,7 @@ public void executeVoid(VirtualFrame frame) {
148154
// 'ceval.c: import_all_from')
149155
if (!name.startsWith("__")) {
150156
Object attr = readAttribute(frame, importedModule, name);
151-
writeAttribute(frame, globals, name, attr);
157+
writeAttribute(frame, locals, name, attr);
152158
}
153159
}
154160
}
@@ -179,6 +185,22 @@ private CastToJavaStringNode ensureCastToStringNode() {
179185
return castToStringNode;
180186
}
181187

188+
private MaterializeFrameNode ensureGetPFrameNode() {
189+
if (getPFrameNode == null) {
190+
CompilerDirectives.transferToInterpreterAndInvalidate();
191+
getPFrameNode = insert(MaterializeFrameNode.create());
192+
}
193+
return getPFrameNode;
194+
}
195+
196+
private ReadLocalsNode ensureGetLocalsNode() {
197+
if (getLocalsNode == null) {
198+
CompilerDirectives.transferToInterpreterAndInvalidate();
199+
getLocalsNode = insert(ReadLocalsNode.create());
200+
}
201+
return getLocalsNode;
202+
}
203+
182204
private IsBuiltinClassProfile ensureIsAttributeErrorProfile() {
183205
if (isAttributeErrorProfile == null) {
184206
CompilerDirectives.transferToInterpreterAndInvalidate();

0 commit comments

Comments
 (0)