26
26
package com .oracle .graal .python .builtins ;
27
27
28
28
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .IndentationError ;
29
+ import static com .oracle .graal .python .builtins .PythonBuiltinClassType .SyntaxError ;
29
30
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .TabError ;
30
31
import static com .oracle .graal .python .builtins .objects .exception .SyntaxErrorBuiltins .SYNTAX_ERROR_ATTR_FACTORY ;
31
32
import static com .oracle .graal .python .nodes .BuiltinNames .MODULES ;
32
33
import static com .oracle .graal .python .nodes .BuiltinNames .PRINT ;
33
34
import static com .oracle .graal .python .nodes .SpecialAttributeNames .__PACKAGE__ ;
34
- import static com .oracle .graal .python .runtime . exception . PythonErrorType . SyntaxError ;
35
+ import static com .oracle .graal .python .nodes . SpecialMethodNames . __REPR__ ;
35
36
36
37
import java .io .IOException ;
37
38
import java .math .BigInteger ;
225
226
import com .oracle .graal .python .builtins .objects .function .BuiltinFunctionBuiltins ;
226
227
import com .oracle .graal .python .builtins .objects .function .FunctionBuiltins ;
227
228
import com .oracle .graal .python .builtins .objects .function .PArguments ;
229
+ import com .oracle .graal .python .builtins .objects .function .PFunction ;
228
230
import com .oracle .graal .python .builtins .objects .function .PBuiltinFunction ;
229
231
import com .oracle .graal .python .builtins .objects .generator .GeneratorBuiltins ;
230
232
import com .oracle .graal .python .builtins .objects .getsetdescriptor .DescriptorBuiltins ;
268
270
import com .oracle .graal .python .builtins .objects .method .DecoratedMethodBuiltins ;
269
271
import com .oracle .graal .python .builtins .objects .method .InstancemethodBuiltins ;
270
272
import com .oracle .graal .python .builtins .objects .method .MethodBuiltins ;
271
- import com .oracle .graal .python .builtins .objects .method .PMethod ;
272
273
import com .oracle .graal .python .builtins .objects .method .StaticmethodBuiltins ;
273
274
import com .oracle .graal .python .builtins .objects .mmap .MMapBuiltins ;
274
275
import com .oracle .graal .python .builtins .objects .module .ModuleBuiltins ;
309
310
import com .oracle .graal .python .builtins .objects .type .TypeBuiltins ;
310
311
import com .oracle .graal .python .builtins .objects .type .TypeNodes .GetNameNode ;
311
312
import com .oracle .graal .python .builtins .objects .zipimporter .ZipImporterBuiltins ;
313
+ import com .oracle .graal .python .lib .PyObjectCallMethodObjArgs ;
312
314
import com .oracle .graal .python .lib .PyObjectLookupAttr ;
313
315
import com .oracle .graal .python .nodes .BuiltinNames ;
316
+ import com .oracle .graal .python .nodes .attributes .ReadAttributeFromDynamicObjectNode ;
317
+ import com .oracle .graal .python .nodes .attributes .WriteAttributeToDynamicObjectNode ;
314
318
import com .oracle .graal .python .nodes .call .GenericInvokeNode ;
315
319
import com .oracle .graal .python .runtime .PythonCodeSerializer ;
316
320
import com .oracle .graal .python .runtime .PythonContext ;
@@ -354,9 +358,6 @@ private static String[] initializeCoreFiles() {
354
358
// Order matters!
355
359
List <String > coreFiles = new ArrayList <>(Arrays .asList (
356
360
"type" ,
357
- "_imp" ,
358
- "function" ,
359
- "_frozen_importlib" ,
360
361
"__graalpython__" ,
361
362
"_weakref" ,
362
363
"faulthandler" ,
@@ -383,8 +384,6 @@ private static String[] initializeCoreFiles() {
383
384
}
384
385
}
385
386
}
386
- // must be last
387
- coreFiles .add ("final_patches" );
388
387
return coreFiles .toArray (new String [coreFiles .size ()]);
389
388
}
390
389
@@ -716,7 +715,7 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
716
715
@ CompilationFinal private PythonModule builtinsModule ;
717
716
@ CompilationFinal private PythonModule sysModule ;
718
717
@ CompilationFinal private PDict sysModules ;
719
- @ CompilationFinal private PMethod importFunc ;
718
+ @ CompilationFinal private PFunction importFunc ;
720
719
@ CompilationFinal private PythonModule importlib ;
721
720
722
721
@ CompilationFinal private PInt pyTrue ;
@@ -811,6 +810,7 @@ public final boolean isCoreInitialized() {
811
810
public final void initialize (PythonContext context ) {
812
811
objectFactory = new PythonObjectSlowPathFactory (context .getAllocationReporter (), context .getLanguage ());
813
812
initializeJavaCore ();
813
+ initializeImportlib ();
814
814
initializePython3Core (context .getCoreHomeOrFail ());
815
815
assert SpecialMethodSlot .checkSlotOverrides (this );
816
816
initialized = true ;
@@ -824,6 +824,42 @@ private void initializeJavaCore() {
824
824
builtinsModule = builtinModules .get (BuiltinNames .BUILTINS );
825
825
}
826
826
827
+ private void initializeImportlib () {
828
+ PythonModule bootstrap = (PythonModule ) ImpModuleBuiltins .importFrozenModuleObject (this , "_frozen_importlib" , false );
829
+
830
+ PyObjectCallMethodObjArgs callNode = PyObjectCallMethodObjArgs .getUncached ();
831
+ WriteAttributeToDynamicObjectNode writeNode = WriteAttributeToDynamicObjectNode .getUncached ();
832
+ ReadAttributeFromDynamicObjectNode readNode = ReadAttributeFromDynamicObjectNode .getUncached ();
833
+
834
+ if (bootstrap == null ) {
835
+ // true when the frozen module is not available
836
+ PythonModule bootstrapExternal = createModule ("importlib._bootstrap_external" );
837
+ writeNode .execute (bootstrapExternal , __PACKAGE__ , "importlib" );
838
+ addBuiltinModule ("_frozen_importlib_external" , bootstrapExternal );
839
+ bootstrap = createModule ("importlib._bootstrap" );
840
+ writeNode .execute (bootstrap , __PACKAGE__ , "importlib" );
841
+ addBuiltinModule ("_frozen_importlib" , bootstrap );
842
+ loadFile ("importlib/_bootstrap_external" , getContext ().getStdlibHome (), bootstrapExternal );
843
+ loadFile ("importlib/_bootstrap" , getContext ().getStdlibHome (), bootstrap );
844
+ }
845
+
846
+ callNode .execute (null , bootstrap , "_install" , getSysModule (), lookupBuiltinModule ("_imp" ));
847
+ writeNode .execute (getBuiltins (), "__import__" , readNode .execute (bootstrap , "__import__" ));
848
+ callNode .execute (null , bootstrap , "_install_external_importers" );
849
+ importFunc = (PFunction ) readNode .execute (bootstrap , "__import__" );
850
+ importlib = bootstrap ;
851
+
852
+ PythonBuiltinClass moduleType = lookupType (PythonBuiltinClassType .PythonModule );
853
+ writeNode .execute (moduleType , __REPR__ , readNode .execute (bootstrap , "_module_repr" ));
854
+ SpecialMethodSlot .reinitializeSpecialMethodSlots (moduleType , getLanguage ());
855
+
856
+ // __package__ needs to be set and doesn't get set by _bootstrap setup
857
+ writeNode .execute (bootstrap , __PACKAGE__ , "importlib" );
858
+
859
+ PythonModule bootstrapExternal = (PythonModule ) getSysModules ().getItem ("_frozen_importlib_external" );
860
+ writeNode .execute (bootstrapExternal , __PACKAGE__ , "importlib" );
861
+ }
862
+
827
863
private void initializePython3Core (String coreHome ) {
828
864
loadFile (BuiltinNames .BUILTINS , coreHome );
829
865
for (String s : coreFiles ) {
@@ -890,24 +926,10 @@ public final PythonModule getImportlib() {
890
926
return importlib ;
891
927
}
892
928
893
- public final void registerImportlib (PythonModule mod ) {
894
- if (importlib != null ) {
895
- throw new IllegalStateException ("importlib cannot be registered more than once" );
896
- }
897
- importlib = mod ;
898
- }
899
-
900
- public final PMethod getImportFunc () {
929
+ public final PFunction getImportFunc () {
901
930
return importFunc ;
902
931
}
903
932
904
- public final void registerImportFunc (PMethod func ) {
905
- if (importFunc != null ) {
906
- throw new IllegalStateException ("__import__ func cannot be registered more than once" );
907
- }
908
- importFunc = func ;
909
- }
910
-
911
933
@ Override
912
934
@ TruffleBoundary
913
935
public final void warn (PythonBuiltinClassType type , String format , Object ... args ) {
@@ -1005,13 +1027,6 @@ private void populateBuiltins() {
1005
1027
// core machinery
1006
1028
sysModule = builtinModules .get ("sys" );
1007
1029
sysModules = (PDict ) sysModule .getAttribute (MODULES );
1008
-
1009
- PythonModule bootstrapExternal = createModule ("importlib._bootstrap_external" );
1010
- bootstrapExternal .setAttribute (__PACKAGE__ , "importlib" );
1011
- addBuiltinModule ("_frozen_importlib_external" , bootstrapExternal );
1012
- PythonModule bootstrap = createModule ("importlib._bootstrap" );
1013
- bootstrap .setAttribute (__PACKAGE__ , "importlib" );
1014
- addBuiltinModule ("_frozen_importlib" , bootstrap );
1015
1030
}
1016
1031
1017
1032
public PythonModule createModule (String name ) {
@@ -1087,16 +1102,20 @@ private Source getInternalSource(String basename, String prefix) {
1087
1102
}
1088
1103
1089
1104
private void loadFile (String s , String prefix ) {
1090
- Supplier <CallTarget > getCode = () -> {
1091
- Source source = getInternalSource (s , prefix );
1092
- return PythonUtils .getOrCreateCallTarget ((RootNode ) getParser ().parse (ParserMode .File , 0 , this , source , null , null ));
1093
- };
1094
- RootCallTarget callTarget = (RootCallTarget ) getLanguage ().cacheCode (s , getCode );
1095
1105
PythonModule mod = lookupBuiltinModule (s );
1096
1106
if (mod == null ) {
1097
1107
// use an anonymous module for the side-effects
1098
1108
mod = factory ().createPythonModule ("__anonymous__" );
1099
1109
}
1110
+ loadFile (s , prefix , mod );
1111
+ }
1112
+
1113
+ private void loadFile (String s , String prefix , PythonModule mod ) {
1114
+ Supplier <CallTarget > getCode = () -> {
1115
+ Source source = getInternalSource (s , prefix );
1116
+ return PythonUtils .getOrCreateCallTarget ((RootNode ) getParser ().parse (ParserMode .File , 0 , this , source , null , null ));
1117
+ };
1118
+ RootCallTarget callTarget = (RootCallTarget ) getLanguage ().cacheCode (s , getCode );
1100
1119
GenericInvokeNode .getUncached ().execute (callTarget , PArguments .withGlobals (mod ));
1101
1120
}
1102
1121
0 commit comments