32
32
import static com .oracle .graal .python .nodes .BuiltinNames .T_NT ;
33
33
import static com .oracle .graal .python .nodes .BuiltinNames .T_STDERR ;
34
34
import static com .oracle .graal .python .nodes .BuiltinNames .T_SYS ;
35
+ import static com .oracle .graal .python .nodes .BuiltinNames .T_ZIPIMPORT ;
35
36
import static com .oracle .graal .python .nodes .BuiltinNames .T__WEAKREF ;
36
37
import static com .oracle .graal .python .nodes .BuiltinNames .T___BUILTINS__ ;
37
38
import static com .oracle .graal .python .nodes .BuiltinNames .T___IMPORT__ ;
45
46
import static com .oracle .graal .python .util .PythonUtils .tsLiteral ;
46
47
47
48
import java .io .IOException ;
49
+ import java .io .StringWriter ;
50
+ import java .io .PrintWriter ;
48
51
import java .math .BigInteger ;
49
52
import java .util .ArrayList ;
50
53
import java .util .Arrays ;
373
376
import com .oracle .graal .python .runtime .PythonContext ;
374
377
import com .oracle .graal .python .runtime .PythonImageBuildOptions ;
375
378
import com .oracle .graal .python .runtime .PythonOptions ;
379
+ import com .oracle .graal .python .runtime .exception .ExceptionUtils ;
376
380
import com .oracle .graal .python .runtime .exception .PException ;
377
381
import com .oracle .graal .python .runtime .interop .PythonMapScope ;
378
382
import com .oracle .graal .python .runtime .object .PythonObjectSlowPathFactory ;
@@ -914,6 +918,7 @@ private void initializeJavaCore() {
914
918
private void initializeImportlib () {
915
919
PythonModule bootstrap = ImpModuleBuiltins .importFrozenModuleObject (this , T__FROZEN_IMPORTLIB , false );
916
920
PythonModule bootstrapExternal ;
921
+ boolean useFrozenModules = bootstrap != null ;
917
922
918
923
PyObjectCallMethodObjArgs callNode = PyObjectCallMethodObjArgs .getUncached ();
919
924
WriteAttributeToPythonObjectNode writeNode = WriteAttributeToPythonObjectNode .getUncached ();
@@ -923,8 +928,7 @@ private void initializeImportlib() {
923
928
// first, a workaround since postInitialize hasn't run yet for the _weakref module aliases
924
929
writeNode .execute (lookupBuiltinModule (T__WEAKREF ), T_REF , lookupType (PythonBuiltinClassType .PReferenceType ));
925
930
926
- if (bootstrap == null ) {
927
- // true when the frozen module is not available
931
+ if (!useFrozenModules ) {
928
932
bootstrapExternal = createModule (T_IMPORTLIB_BOOTSTRAP_EXTERNAL );
929
933
bootstrap = createModule (T_IMPORTLIB_BOOTSTRAP );
930
934
loadFile (toTruffleStringUncached ("importlib/_bootstrap_external" ), getContext ().getStdlibHome (), bootstrapExternal );
@@ -964,11 +968,25 @@ private void initializeImportlib() {
964
968
LOGGER .log (Level .FINE , () -> "initializing zipimport failed" );
965
969
} else {
966
970
LOGGER .log (Level .FINE , () -> "# installing zipimport hook" );
971
+ // when frozen modules are not used, we need to load from file directly
967
972
PythonModule zipimport = null ;
968
973
try {
969
- zipimport = AbstractImportNode .importModule (toTruffleStringUncached ("zipimport" ));
970
- } catch (PException e ) {
971
- LOGGER .log (Level .FINE , () -> "# can't import zipimport" );
974
+ if (useFrozenModules ) {
975
+ zipimport = AbstractImportNode .importModule (T_ZIPIMPORT );
976
+ } else {
977
+ // load zipimport from file, and since postInitialize hasn't run yet we
978
+ // cannot use the normal import machinery without frozen modules at this
979
+ // point
980
+ zipimport = createModule (T_ZIPIMPORT );
981
+ loadFile (T_ZIPIMPORT , getContext ().getStdlibHome (), zipimport );
982
+ setItem .execute (null , null , sysModules , T_ZIPIMPORT , zipimport );
983
+ }
984
+ } catch (RuntimeException e ) {
985
+ LOGGER .log (Level .FINE , () -> {
986
+ StringWriter tb = new StringWriter ();
987
+ ExceptionUtils .printPythonLikeStackTrace (new PrintWriter (tb ), e );
988
+ return tb .toString () + System .lineSeparator () + "# can't import zipimport" ;
989
+ });
972
990
}
973
991
if (zipimport != null ) {
974
992
writeNode .execute (zipimport , T___BUILTINS__ , getBuiltins ());
0 commit comments