Skip to content

Commit 3cc95c2

Browse files
committed
refactor import nodes
1 parent beaff32 commit 3cc95c2

File tree

4 files changed

+67
-49
lines changed

4 files changed

+67
-49
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public abstract class ErrorMessages {
127127
public static final String CANNOT_GET_SHAPE_OF_NATIVE_CLS = "cannot get shape of native class";
128128
public static final String CANNOT_GET_CONSISTEMT_METHOD_RESOLUTION = "Cannot create a consistent method resolution\norder (MRO) for bases %s";
129129
public static final String CANNOT_HANDLE_ZIP_FILE = "cannot handle Zip file: '%s'";
130+
public static final String IMPORT_NOT_FOUND = "__import__ not found";
130131
public static final String CANNOT_IMPORT_NAME = "cannot import name '%s' from '%s' (%s)";
131132
public static final String CANNOT_IMPORT_NAME_CIRCULAR = "cannot import name '%s' from partially initialized module '%s' (most likely due to a circular import)";
132133
public static final String CANNOT_INITIALIZE_WITH = "cannot initialize %s with %s%s";

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

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,46 @@
4141
package com.oracle.graal.python.nodes.statement;
4242

4343
import static com.oracle.graal.python.nodes.BuiltinNames.__IMPORT__;
44+
import static com.oracle.graal.python.nodes.ErrorMessages.IMPORT_NOT_FOUND;
4445

4546
import com.oracle.graal.python.PythonLanguage;
47+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4648
import com.oracle.graal.python.builtins.objects.PNone;
49+
import com.oracle.graal.python.builtins.objects.function.PFunction;
4750
import com.oracle.graal.python.builtins.objects.function.PKeyword;
4851
import com.oracle.graal.python.builtins.objects.method.PMethod;
4952
import com.oracle.graal.python.builtins.objects.module.PythonModule;
53+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5054
import com.oracle.graal.python.builtins.objects.str.PString;
5155
import com.oracle.graal.python.nodes.BuiltinNames;
56+
import com.oracle.graal.python.nodes.PRaiseImportErrorNode;
57+
import com.oracle.graal.python.nodes.PRaiseImportErrorNodeGen;
58+
import com.oracle.graal.python.nodes.PRaiseNode;
5259
import com.oracle.graal.python.nodes.call.CallNode;
5360
import com.oracle.graal.python.nodes.object.GetDictNode;
5461
import com.oracle.graal.python.runtime.PythonContext;
5562
import com.oracle.graal.python.runtime.PythonOptions;
63+
import com.oracle.graal.python.runtime.exception.PException;
5664
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5765
import com.oracle.graal.python.util.PythonUtils;
5866
import com.oracle.truffle.api.CompilerDirectives;
5967
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
6068
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6169
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
6270
import com.oracle.truffle.api.TruffleLanguage.LanguageReference;
71+
import com.oracle.truffle.api.frame.Frame;
6372
import com.oracle.truffle.api.frame.VirtualFrame;
6473
import com.oracle.truffle.api.instrumentation.StandardTags;
6574
import com.oracle.truffle.api.instrumentation.Tag;
6675

6776
public abstract class AbstractImportNode extends StatementNode {
6877
@Child PythonObjectFactory objectFactory;
78+
@Child private PythonObjectLibrary pythonLibrary;
6979

7080
@Child private CallNode callNode;
7181
@Child private GetDictNode getDictNode;
82+
@Child private PRaiseNode raiseNode;
83+
@Child private PRaiseImportErrorNode raiseImportErrorNode;
7284

7385
@CompilationFinal private LanguageReference<PythonLanguage> languageRef;
7486
@CompilationFinal private ContextReference<PythonContext> contextRef;
@@ -109,6 +121,46 @@ protected Object importModule(VirtualFrame frame, String name) {
109121
return importModule(frame, name, PNone.NONE, PythonUtils.EMPTY_STRING_ARRAY, 0);
110122
}
111123

124+
protected PythonObjectLibrary ensurePythonLibrary() {
125+
if (pythonLibrary == null) {
126+
CompilerDirectives.transferToInterpreterAndInvalidate();
127+
pythonLibrary = insert(PythonObjectLibrary.getFactory().createDispatched(PythonOptions.getCallSiteInlineCacheMaxDepth()));
128+
}
129+
return pythonLibrary;
130+
}
131+
132+
private PRaiseNode ensureRaiseNode() {
133+
if (raiseNode == null) {
134+
CompilerDirectives.transferToInterpreterAndInvalidate();
135+
raiseNode = insert(PRaiseNode.create());
136+
}
137+
return raiseNode;
138+
}
139+
140+
protected PException raiseTypeError(String format, Object... args) {
141+
throw raise(PythonBuiltinClassType.TypeError, format, args);
142+
}
143+
144+
protected PException raise(PythonBuiltinClassType type, String format, Object... args) {
145+
throw ensureRaiseNode().raise(type, format, args);
146+
}
147+
148+
private PRaiseImportErrorNode ensureRaiseImportErrorNode() {
149+
if (raiseImportErrorNode == null) {
150+
CompilerDirectives.transferToInterpreterAndInvalidate();
151+
raiseImportErrorNode = insert(PRaiseImportErrorNode.create());
152+
}
153+
return raiseImportErrorNode;
154+
}
155+
156+
protected PException raiseImportError(Frame frame, String format, Object... formatArgs) {
157+
throw raiseImportError(frame, PNone.NO_VALUE, PNone.NO_VALUE, format, formatArgs);
158+
}
159+
160+
protected PException raiseImportError(Frame frame, Object name, Object path, String format, Object... formatArgs) {
161+
throw ensureRaiseImportErrorNode().raiseImportError(frame, name, path, format, formatArgs);
162+
}
163+
112164
CallNode getCallNode() {
113165
if (callNode == null) {
114166
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -131,7 +183,8 @@ public static Object importModule(String name) {
131183
CallNode callNode = CallNode.getUncached();
132184
GetDictNode getDictNode = GetDictNode.getUncached();
133185
PythonObjectFactory factory = PythonObjectFactory.getUncached();
134-
return __import__(null, ctx, name, PNone.NONE, PythonUtils.EMPTY_STRING_ARRAY, 0, callNode, getDictNode, factory);
186+
PRaiseImportErrorNode raiseNode = PRaiseImportErrorNodeGen.getUncached();
187+
return __import__(null, raiseNode, ctx, name, PNone.NONE, PythonUtils.EMPTY_STRING_ARRAY, 0, callNode, getDictNode, factory);
135188
}
136189

137190
protected Object importModule(VirtualFrame frame, String name, Object globals, String[] fromList, int level) {
@@ -151,17 +204,22 @@ protected Object importModule(VirtualFrame frame, String name, Object globals, S
151204
}
152205
}
153206
try {
154-
return __import__(frame, context, name, globals, fromList, level, getCallNode(), getGetDictNode(), factory());
207+
return __import__(frame, ensureRaiseImportErrorNode(), context, name, globals, fromList, level, getCallNode(), getGetDictNode(), factory());
155208
} finally {
156209
if (emulateJython()) {
157210
context.popCurrentImport();
158211
}
159212
}
160213
}
161214

162-
private static Object __import__(VirtualFrame frame, PythonContext ctx, String name, Object globals, String[] fromList, int level, CallNode callNode, GetDictNode getDictNode,
215+
private static Object __import__(VirtualFrame frame, PRaiseImportErrorNode raiseNode, PythonContext ctx, String name, Object globals, String[] fromList, int level, CallNode callNode,
216+
GetDictNode getDictNode,
163217
PythonObjectFactory factory) {
164-
PMethod builtinImport = (PMethod) ctx.getCore().lookupBuiltinModule(BuiltinNames.BUILTINS).getAttribute(__IMPORT__);
218+
Object builtinImport = ctx.getCore().lookupBuiltinModule(BuiltinNames.BUILTINS).getAttribute(__IMPORT__);
219+
if (builtinImport == PNone.NO_VALUE) {
220+
throw raiseNode.raiseImportError(frame, IMPORT_NOT_FOUND);
221+
}
222+
assert builtinImport instanceof PMethod || builtinImport instanceof PFunction;
165223
assert fromList != null;
166224
assert globals != null;
167225
// the locals argument is ignored so it can always be None

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
*/
2626
package com.oracle.graal.python.nodes.statement;
2727

28+
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__FILE__;
2829
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
29-
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__PATH__;
3030
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__SPEC__;
3131

3232
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
@@ -36,11 +36,9 @@
3636
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3737
import com.oracle.graal.python.builtins.objects.str.PString;
3838
import com.oracle.graal.python.nodes.ErrorMessages;
39-
import com.oracle.graal.python.nodes.PRaiseImportErrorNode;
4039
import com.oracle.graal.python.nodes.frame.WriteNode;
4140
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
4241
import com.oracle.graal.python.nodes.subscript.GetItemNode;
43-
import com.oracle.graal.python.runtime.PythonOptions;
4442
import com.oracle.graal.python.runtime.exception.PException;
4543
import com.oracle.truffle.api.CompilerDirectives;
4644
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -50,9 +48,7 @@
5048

5149
public class ImportFromNode extends AbstractImportNode {
5250
@Children private final WriteNode[] aslist;
53-
@Child private PythonObjectLibrary pythonLibrary;
5451
@Child private GetItemNode getItem;
55-
@Child private PRaiseImportErrorNode raiseNode;
5652

5753
private final String importee;
5854
private final int level;
@@ -118,16 +114,16 @@ public void executeVoid(VirtualFrame frame) {
118114
Object modulePath = "unknown location";
119115
if (!getAttrErrorProfile.profileException(e2, PythonBuiltinClassType.AttributeError)) {
120116
try {
121-
modulePath = pol.lookupAttributeStrict(importedModule, frame, __PATH__);
117+
modulePath = pol.lookupAttributeStrict(importedModule, frame, __FILE__);
122118
} catch (PException e3) {
123119
e3.expectAttributeError(getFileErrorProfile);
124120
}
125121
}
126122

127123
if (isModuleInitialising(frame, pol, importedModule)) {
128-
throw ensureRaiseNode().raiseImportError(frame, moduleName, modulePath, ErrorMessages.CANNOT_IMPORT_NAME_CIRCULAR, attr, moduleName);
124+
throw raiseImportError(frame, moduleName, modulePath, ErrorMessages.CANNOT_IMPORT_NAME_CIRCULAR, attr, moduleName);
129125
} else {
130-
throw ensureRaiseNode().raiseImportError(frame, moduleName, modulePath, ErrorMessages.CANNOT_IMPORT_NAME, attr, moduleName, modulePath);
126+
throw raiseImportError(frame, moduleName, modulePath, ErrorMessages.CANNOT_IMPORT_NAME, attr, moduleName, modulePath);
131127
}
132128
}
133129
}
@@ -143,22 +139,6 @@ private boolean isModuleInitialising(VirtualFrame frame, PythonObjectLibrary pol
143139
return false;
144140
}
145141

146-
private PythonObjectLibrary ensurePythonLibrary() {
147-
if (pythonLibrary == null) {
148-
CompilerDirectives.transferToInterpreterAndInvalidate();
149-
pythonLibrary = insert(PythonObjectLibrary.getFactory().createDispatched(PythonOptions.getCallSiteInlineCacheMaxDepth()));
150-
}
151-
return pythonLibrary;
152-
}
153-
154-
private PRaiseImportErrorNode ensureRaiseNode() {
155-
if (raiseNode == null) {
156-
CompilerDirectives.transferToInterpreterAndInvalidate();
157-
raiseNode = insert(PRaiseImportErrorNode.create());
158-
}
159-
return raiseNode;
160-
}
161-
162142
private GetItemNode ensureGetItemNode() {
163143
if (getItem == null) {
164144
CompilerDirectives.transferToInterpreterAndInvalidate();

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DICT__;
3030
import static com.oracle.graal.python.nodes.frame.ReadLocalsNode.fastGetCustomLocalsOrGlobals;
3131

32-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3332
import com.oracle.graal.python.builtins.objects.dict.PDict;
3433
import com.oracle.graal.python.builtins.objects.function.PArguments;
3534
import com.oracle.graal.python.builtins.objects.mappingproxy.PMappingproxy;
@@ -38,7 +37,6 @@
3837
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3938
import com.oracle.graal.python.builtins.objects.str.PString;
4039
import com.oracle.graal.python.nodes.ErrorMessages;
41-
import com.oracle.graal.python.nodes.PRaiseNode;
4240
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
4341
import com.oracle.graal.python.nodes.control.GetNextNode;
4442
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
@@ -47,7 +45,6 @@
4745
import com.oracle.graal.python.nodes.util.CannotCastException;
4846
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
4947
import com.oracle.graal.python.nodes.util.CastToJavaStringNodeGen;
50-
import com.oracle.graal.python.runtime.PythonOptions;
5148
import com.oracle.graal.python.runtime.exception.PException;
5249
import com.oracle.truffle.api.CompilerDirectives;
5350
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -65,10 +62,8 @@ public class ImportStarNode extends AbstractImportNode {
6562
@Child private SetItemNode dictWriteNode;
6663
@Child private SetAttributeNode.Dynamic setAttributeNode;
6764
@Child private GetItemNode getItemNode;
68-
@Child private PythonObjectLibrary pythonLibrary;
6965
@Child private CastToJavaStringNode castToStringNode;
7066
@Child private GetNextNode nextNode;
71-
@Child private PRaiseNode raiseNode;
7267

7368
@Child private IsBuiltinClassProfile isAttributeErrorProfile;
7469
@Child private IsBuiltinClassProfile isStopIterationProfile;
@@ -160,14 +155,6 @@ private void writeAttributeToLocals(VirtualFrame frame, PythonObjectLibrary pol,
160155
}
161156
}
162157

163-
private PythonObjectLibrary ensurePythonLibrary() {
164-
if (pythonLibrary == null) {
165-
CompilerDirectives.transferToInterpreterAndInvalidate();
166-
pythonLibrary = insert(PythonObjectLibrary.getFactory().createDispatched(PythonOptions.getCallSiteInlineCacheMaxDepth()));
167-
}
168-
return pythonLibrary;
169-
}
170-
171158
private GetItemNode ensureGetItemNode() {
172159
if (getItemNode == null) {
173160
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -200,14 +187,6 @@ private IsBuiltinClassProfile ensureIsStopIterationErrorProfile() {
200187
return isStopIterationProfile;
201188
}
202189

203-
private PException raiseTypeError(String format, Object... args) {
204-
if (raiseNode == null) {
205-
CompilerDirectives.transferToInterpreterAndInvalidate();
206-
raiseNode = insert(PRaiseNode.create());
207-
}
208-
throw raiseNode.raise(PythonBuiltinClassType.TypeError, format, args);
209-
}
210-
211190
private GetNextNode ensureGetNextNode() {
212191
if (nextNode == null) {
213192
CompilerDirectives.transferToInterpreterAndInvalidate();

0 commit comments

Comments
 (0)