Skip to content

Commit 1c4afc6

Browse files
committed
ensure encodings are imported before loading _codecs_truffle.py
1 parent 20a5dce commit 1c4afc6

File tree

2 files changed

+20
-49
lines changed

2 files changed

+20
-49
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,10 @@
243243
import com.oracle.graal.python.builtins.objects.type.TypeBuiltins;
244244
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
245245
import com.oracle.graal.python.builtins.objects.zipimporter.ZipImporterBuiltins;
246+
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
246247
import com.oracle.graal.python.nodes.BuiltinNames;
247248
import com.oracle.graal.python.nodes.call.GenericInvokeNode;
249+
import com.oracle.graal.python.nodes.statement.AbstractImportNode;
248250
import com.oracle.graal.python.runtime.PythonCodeSerializer;
249251
import com.oracle.graal.python.runtime.PythonContext;
250252
import com.oracle.graal.python.runtime.PythonOptions;
@@ -301,7 +303,9 @@ private static String[] initializeCoreFiles() {
301303
"base_exception",
302304
PythonCextBuiltins.PYTHON_CEXT,
303305
"_collections",
304-
"_codecs",
306+
// TODO: see the encodings initialization before _codecs_truffle.py is
307+
// loaded in initializePython3Core;
308+
// once _codecs_truffle.py is gone, it should not be necessary
305309
"_codecs_truffle",
306310
"bytes",
307311
"bytearray",
@@ -654,11 +658,26 @@ private void initializeJavaCore() {
654658
private void initializePython3Core(String coreHome) {
655659
loadFile(BuiltinNames.BUILTINS, coreHome);
656660
for (String s : coreFiles) {
661+
// TODO: once _codecs_truffle.py is gone, this should not be necessary
662+
if (s.equals("_codecs_truffle")) {
663+
importEncoding();
664+
}
657665
loadFile(s, coreHome);
658666
}
659667
initialized = true;
660668
}
661669

670+
private void importEncoding() {
671+
PythonModule sys = lookupBuiltinModule("sys");
672+
Object sysPath = sys.getAttribute("path");
673+
PyObjectCallMethodObjArgs.getUncached().execute(null, sysPath, "insert", 0, getContext().getStdlibHome());
674+
try {
675+
AbstractImportNode.importModule("encodings");
676+
} finally {
677+
PyObjectCallMethodObjArgs.getUncached().execute(null, sysPath, "pop");
678+
}
679+
}
680+
662681
/**
663682
* Run post-initialization code that needs a fully working Python environment. This will be run
664683
* eagerly when the context is initialized on the JVM or a new context is created on SVM, but is

graalpython/lib-graalpython/_codecs.py

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

0 commit comments

Comments
 (0)