Skip to content

Commit e85cb52

Browse files
committed
finalize importlib freezing
- fix generating FrozenModules.java for the special case of an stdlib module that has only alias definitions - remove _imp, _frozen_importlib, and final_patches
1 parent abfb95b commit e85cb52

File tree

7 files changed

+35
-233
lines changed

7 files changed

+35
-233
lines changed

graalpython/com.oracle.graal.python.frozen/freeze_modules.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"ntpath",
7070
"posixpath",
7171
# We must explicitly mark os.path as a frozen module
72-
f'{"ntpath" if os.name == "nt" else "posixpath"} : os.path",
72+
("ntpath" if os.name == "nt" else "posixpath") + " : os.path",
7373
"os",
7474
"site",
7575
"stat",
@@ -526,7 +526,10 @@ def freeze_module(src):
526526
def write_frozen_modules_map(out_file, modules):
527527
out_file.write(" private static final class Map {\n")
528528
for module in modules:
529-
if not module.isalias or not module.orig:
529+
if (not module.isalias or
530+
not module.orig or
531+
not any(module.orig == m.orig for m in modules if m != module)
532+
):
530533
ispkg = "true" if module.ispkg else "false"
531534
out_file.write(
532535
f' private static final PythonFrozenModule {module.symbol} = new PythonFrozenModule("{module.symbol}", "{module.frozenid}", {ispkg});\n'

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
package com.oracle.graal.python.builtins;
2727

2828
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.IndentationError;
29+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SyntaxError;
2930
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TabError;
3031
import static com.oracle.graal.python.builtins.objects.exception.SyntaxErrorBuiltins.SYNTAX_ERROR_ATTR_FACTORY;
3132
import static com.oracle.graal.python.nodes.BuiltinNames.MODULES;
3233
import static com.oracle.graal.python.nodes.BuiltinNames.PRINT;
3334
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__;
3536

3637
import java.io.IOException;
3738
import java.math.BigInteger;
@@ -357,9 +358,7 @@ private static String[] initializeCoreFiles() {
357358
// Order matters!
358359
List<String> coreFiles = new ArrayList<>(Arrays.asList(
359360
"type",
360-
"_imp",
361361
"function",
362-
"_frozen_importlib",
363362
"__graalpython__",
364363
"_weakref",
365364
"faulthandler",
@@ -386,8 +385,6 @@ private static String[] initializeCoreFiles() {
386385
}
387386
}
388387
}
389-
// must be last
390-
coreFiles.add("final_patches");
391388
return coreFiles.toArray(new String[coreFiles.size()]);
392389
}
393390

@@ -829,7 +826,7 @@ private void initializeJavaCore() {
829826
}
830827

831828
private void initializeImportlib() {
832-
PythonModule bootstrap = (PythonModule) ImpModuleBuiltins.importFrozenModuleObject(this, "_frozen_importlib");
829+
PythonModule bootstrap = (PythonModule) ImpModuleBuiltins.importFrozenModuleObject(this, "_frozen_importlib", false);
833830

834831
PyObjectCallMethodObjArgs callNode = PyObjectCallMethodObjArgs.getUncached();
835832
WriteAttributeToDynamicObjectNode writeNode = WriteAttributeToDynamicObjectNode.getUncached();
@@ -843,8 +840,8 @@ private void initializeImportlib() {
843840
bootstrap = createModule("importlib._bootstrap");
844841
writeNode.execute(bootstrap, __PACKAGE__, "importlib");
845842
addBuiltinModule("_frozen_importlib", bootstrap);
846-
loadFile("importlib/_bootstrap_external.py", getContext().getStdlibHome());
847-
loadFile("importlib/_bootstrap.py", getContext().getStdlibHome());
843+
loadFile("importlib/_bootstrap_external", getContext().getStdlibHome(), bootstrapExternal);
844+
loadFile("importlib/_bootstrap", getContext().getStdlibHome(), bootstrap);
848845
}
849846

850847
callNode.execute(null, bootstrap, "_install", getSysModule(), lookupBuiltinModule("_imp"));
@@ -853,8 +850,11 @@ private void initializeImportlib() {
853850
importFunc = (PFunction) readNode.execute(bootstrap, "__import__");
854851
importlib = bootstrap;
855852

853+
writeNode.execute(lookupType(PythonBuiltinClassType.PythonModule), __REPR__, readNode.execute(bootstrap, "_module_repr"));
854+
856855
// __package__ needs to be set and doesn't get set by _bootstrap setup
857856
writeNode.execute(bootstrap, __PACKAGE__, "importlib");
857+
858858
PythonModule bootstrapExternal = (PythonModule) getSysModules().getItem("_frozen_importlib_external");
859859
writeNode.execute(bootstrapExternal, __PACKAGE__, "importlib");
860860
}
@@ -1101,16 +1101,20 @@ private Source getInternalSource(String basename, String prefix) {
11011101
}
11021102

11031103
private void loadFile(String s, String prefix) {
1104-
Supplier<CallTarget> getCode = () -> {
1105-
Source source = getInternalSource(s, prefix);
1106-
return PythonUtils.getOrCreateCallTarget((RootNode) getParser().parse(ParserMode.File, 0, this, source, null, null));
1107-
};
1108-
RootCallTarget callTarget = (RootCallTarget) getLanguage().cacheCode(s, getCode);
11091104
PythonModule mod = lookupBuiltinModule(s);
11101105
if (mod == null) {
11111106
// use an anonymous module for the side-effects
11121107
mod = factory().createPythonModule("__anonymous__");
11131108
}
1109+
loadFile(s, prefix, mod);
1110+
}
1111+
1112+
private void loadFile(String s, String prefix, PythonModule mod) {
1113+
Supplier<CallTarget> getCode = () -> {
1114+
Source source = getInternalSource(s, prefix);
1115+
return PythonUtils.getOrCreateCallTarget((RootNode) getParser().parse(ParserMode.File, 0, this, source, null, null));
1116+
};
1117+
RootCallTarget callTarget = (RootCallTarget) getLanguage().cacheCode(s, getCode);
11141118
GenericInvokeNode.getUncached().execute(callTarget, PArguments.withGlobals(mod));
11151119
}
11161120

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ImpModuleBuiltins.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,7 @@ protected ArgumentClinicProvider getArgumentClinic() {
465465

466466
@Specialization
467467
boolean run(String name) {
468-
PythonFrozenModule mod = FrozenModules.lookup(name);
469-
return mod != null && mod.getCode() != null;
468+
return findFrozen(name).status == FROZEN_OKAY;
470469
}
471470
}
472471

@@ -618,7 +617,7 @@ protected ArgumentClinicProvider getArgumentClinic() {
618617

619618
@Specialization
620619
Object run(String name) {
621-
return importFrozenModuleObject(getCore(), name);
620+
return importFrozenModuleObject(getCore(), name, true);
622621
}
623622
}
624623

@@ -627,7 +626,7 @@ Object run(String name) {
627626
* imported module, null, or raises a Python exception.
628627
*/
629628
@TruffleBoundary
630-
public static Object importFrozenModuleObject(Python3Core core, String name) {
629+
public static Object importFrozenModuleObject(Python3Core core, String name, boolean doRaise) {
631630
FrozenResult result = findFrozen(name);
632631
FrozenStatus status = result.status;
633632
FrozenInfo info = result.info;
@@ -638,7 +637,11 @@ public static Object importFrozenModuleObject(Python3Core core, String name) {
638637
case FROZEN_BAD_NAME:
639638
return null;
640639
default:
641-
raiseFrozenError(status, name, PRaiseNode.getUncached());
640+
if (doRaise) {
641+
raiseFrozenError(status, name, PRaiseNode.getUncached());
642+
} else {
643+
return null;
644+
}
642645
}
643646

644647
PCode code = (PCode) MarshalModuleBuiltins.Marshal.load(info.data, info.size);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/module/FrozenModules.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
public final class FrozenModules {
4343

4444
private static final class Map {
45+
private static final PythonFrozenModule IMPORTLIB__BOOTSTRAP = new PythonFrozenModule("IMPORTLIB__BOOTSTRAP", "importlib._bootstrap", false);
46+
private static final PythonFrozenModule IMPORTLIB__BOOTSTRAP_EXTERNAL = new PythonFrozenModule("IMPORTLIB__BOOTSTRAP_EXTERNAL", "importlib._bootstrap_external", false);
4547
private static final PythonFrozenModule ABC = new PythonFrozenModule("ABC", "abc", false);
4648
private static final PythonFrozenModule CODECS = new PythonFrozenModule("CODECS", "codecs", false);
4749
private static final PythonFrozenModule ENCODINGS = new PythonFrozenModule("ENCODINGS", "encodings", true);
@@ -200,6 +202,8 @@ private static final class Map {
200202

201203
public static final PythonFrozenModule lookup(String name) {
202204
switch (name) {
205+
case "_frozen_importlib": return Map.IMPORTLIB__BOOTSTRAP;
206+
case "_frozen_importlib_external": return Map.IMPORTLIB__BOOTSTRAP_EXTERNAL;
203207
case "abc": return Map.ABC;
204208
case "codecs": return Map.CODECS;
205209
case "encodings": return Map.ENCODINGS;
@@ -351,6 +355,7 @@ public static final PythonFrozenModule lookup(String name) {
351355
case "genericpath": return Map.GENERICPATH;
352356
case "ntpath": return Map.NTPATH;
353357
case "posixpath": return Map.POSIXPATH;
358+
case "os.path": return Map.POSIXPATH;
354359
case "os": return Map.OS;
355360
case "site": return Map.SITE;
356361
case "stat": return Map.STAT;

graalpython/lib-graalpython/_frozen_importlib.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

graalpython/lib-graalpython/_imp.py

Lines changed: 0 additions & 113 deletions
This file was deleted.

graalpython/lib-graalpython/final_patches.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)