41
41
package com .oracle .graal .python .nodes .statement ;
42
42
43
43
import static com .oracle .graal .python .nodes .BuiltinNames .__IMPORT__ ;
44
+ import static com .oracle .graal .python .nodes .ErrorMessages .IMPORT_NOT_FOUND ;
44
45
45
46
import com .oracle .graal .python .PythonLanguage ;
47
+ import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
46
48
import com .oracle .graal .python .builtins .objects .PNone ;
49
+ import com .oracle .graal .python .builtins .objects .function .PFunction ;
47
50
import com .oracle .graal .python .builtins .objects .function .PKeyword ;
48
51
import com .oracle .graal .python .builtins .objects .method .PMethod ;
49
52
import com .oracle .graal .python .builtins .objects .module .PythonModule ;
53
+ import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
50
54
import com .oracle .graal .python .builtins .objects .str .PString ;
51
55
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 ;
52
59
import com .oracle .graal .python .nodes .call .CallNode ;
53
60
import com .oracle .graal .python .nodes .object .GetDictNode ;
54
61
import com .oracle .graal .python .runtime .PythonContext ;
55
62
import com .oracle .graal .python .runtime .PythonOptions ;
63
+ import com .oracle .graal .python .runtime .exception .PException ;
56
64
import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
57
65
import com .oracle .graal .python .util .PythonUtils ;
58
66
import com .oracle .truffle .api .CompilerDirectives ;
59
67
import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
60
68
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
61
69
import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
62
70
import com .oracle .truffle .api .TruffleLanguage .LanguageReference ;
71
+ import com .oracle .truffle .api .frame .Frame ;
63
72
import com .oracle .truffle .api .frame .VirtualFrame ;
64
73
import com .oracle .truffle .api .instrumentation .StandardTags ;
65
74
import com .oracle .truffle .api .instrumentation .Tag ;
66
75
67
76
public abstract class AbstractImportNode extends StatementNode {
68
77
@ Child PythonObjectFactory objectFactory ;
78
+ @ Child private PythonObjectLibrary pythonLibrary ;
69
79
70
80
@ Child private CallNode callNode ;
71
81
@ Child private GetDictNode getDictNode ;
82
+ @ Child private PRaiseNode raiseNode ;
83
+ @ Child private PRaiseImportErrorNode raiseImportErrorNode ;
72
84
73
85
@ CompilationFinal private LanguageReference <PythonLanguage > languageRef ;
74
86
@ CompilationFinal private ContextReference <PythonContext > contextRef ;
@@ -109,6 +121,46 @@ protected Object importModule(VirtualFrame frame, String name) {
109
121
return importModule (frame , name , PNone .NONE , PythonUtils .EMPTY_STRING_ARRAY , 0 );
110
122
}
111
123
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
+
112
164
CallNode getCallNode () {
113
165
if (callNode == null ) {
114
166
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -131,7 +183,8 @@ public static Object importModule(String name) {
131
183
CallNode callNode = CallNode .getUncached ();
132
184
GetDictNode getDictNode = GetDictNode .getUncached ();
133
185
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 );
135
188
}
136
189
137
190
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
151
204
}
152
205
}
153
206
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 ());
155
208
} finally {
156
209
if (emulateJython ()) {
157
210
context .popCurrentImport ();
158
211
}
159
212
}
160
213
}
161
214
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 ,
163
217
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 ;
165
223
assert fromList != null ;
166
224
assert globals != null ;
167
225
// the locals argument is ignored so it can always be None
0 commit comments