Skip to content

Commit 0d6da13

Browse files
committed
intrinsified lib-graalpython/sys_post_init.py
1 parent d2bae30 commit 0d6da13

File tree

7 files changed

+101
-102
lines changed

7 files changed

+101
-102
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_imports.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,6 @@ def test_recursive_import_from():
135135
import package.recpkg
136136
assert package.recpkg.context is package.recpkg.reduction.context
137137

138-
139-
if sys.implementation.name == "graalpython":
140-
def test_imp_cached_imports():
141-
import _imp
142-
143-
finder = _imp.CachedImportFinder
144-
145-
spec = finder.find_spec("encodings", None)
146-
assert spec.submodule_search_locations
147-
148-
149138
def test_import_package_all() :
150139
import package1
151140
expected_syms = ["moduleX", "lib1_hello", "lib1_world"]

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,9 @@
246246
import com.oracle.graal.python.builtins.objects.type.TypeBuiltins;
247247
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
248248
import com.oracle.graal.python.builtins.objects.zipimporter.ZipImporterBuiltins;
249-
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
250249
import com.oracle.graal.python.lib.PyObjectLookupAttr;
251250
import com.oracle.graal.python.nodes.BuiltinNames;
252251
import com.oracle.graal.python.nodes.call.GenericInvokeNode;
253-
import com.oracle.graal.python.nodes.statement.AbstractImportNode;
254252
import com.oracle.graal.python.runtime.PythonCodeSerializer;
255253
import com.oracle.graal.python.runtime.PythonContext;
256254
import com.oracle.graal.python.runtime.PythonOptions;
@@ -314,10 +312,6 @@ private static String[] initializeCoreFiles() {
314312
"zipimport",
315313
"mmap",
316314
"java",
317-
// TODO: see the encodings initialization before sys_post_init.py is
318-
// loaded in initializePython3Core;
319-
// once sys_post_init.py is gone, it should not be necessary
320-
"sys_post_init",
321315
"_contextvars",
322316
"pip_hook",
323317
"_struct",
@@ -660,26 +654,11 @@ private void initializeJavaCore() {
660654
private void initializePython3Core(String coreHome) {
661655
loadFile(BuiltinNames.BUILTINS, coreHome);
662656
for (String s : coreFiles) {
663-
// TODO: once sys_post_init.py is gone, this should not be necessary
664-
if (s.equals("sys_post_init")) {
665-
importEncoding();
666-
}
667657
loadFile(s, coreHome);
668658
}
669659
initialized = true;
670660
}
671661

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ public void postInitialize(Python3Core core) {
217217
}
218218
}
219219

220+
@TruffleBoundary
221+
String getStdIOEncoding() {
222+
return (String) builtinConstants.get("stdio_encoding");
223+
}
224+
225+
@TruffleBoundary
226+
String getStdIOError() {
227+
return (String) builtinConstants.get("stdio_error");
228+
}
229+
220230
/**
221231
* Entry point for executing a path using the launcher, e.g. {@code python foo.py}
222232
*/

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

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@
6262
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
6363
import com.oracle.graal.python.builtins.PythonBuiltins;
6464
import com.oracle.graal.python.builtins.modules.SysModuleBuiltinsClinicProviders.GetFrameNodeClinicProviderGen;
65+
import com.oracle.graal.python.builtins.modules.io.BufferedReaderBuiltins;
66+
import com.oracle.graal.python.builtins.modules.io.BufferedWriterBuiltins;
6567
import com.oracle.graal.python.builtins.modules.io.FileIOBuiltins;
68+
import com.oracle.graal.python.builtins.modules.io.PBuffered;
6669
import com.oracle.graal.python.builtins.modules.io.PFileIO;
70+
import com.oracle.graal.python.builtins.modules.io.PTextIO;
71+
import com.oracle.graal.python.builtins.modules.io.TextIOWrapperNodes.TextIOWrapperInitNode;
72+
import com.oracle.graal.python.builtins.modules.io.TextIOWrapperNodesFactory.TextIOWrapperInitNodeGen;
6773
import com.oracle.graal.python.builtins.objects.PNone;
6874
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
6975
import com.oracle.graal.python.builtins.objects.frame.PFrame;
@@ -73,6 +79,7 @@
7379
import com.oracle.graal.python.builtins.objects.list.PList;
7480
import com.oracle.graal.python.builtins.objects.module.PythonModule;
7581
import com.oracle.graal.python.builtins.objects.namespace.PSimpleNamespace;
82+
import com.oracle.graal.python.builtins.objects.object.PythonObject;
7683
import com.oracle.graal.python.builtins.objects.str.PString;
7784
import com.oracle.graal.python.builtins.objects.str.StringNodes;
7885
import com.oracle.graal.python.builtins.objects.traceback.GetTracebackNode;
@@ -81,6 +88,7 @@
8188
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
8289
import com.oracle.graal.python.builtins.objects.tuple.StructSequence;
8390
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
91+
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
8492
import com.oracle.graal.python.nodes.ErrorMessages;
8593
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
8694
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode.NoAttributeHandler;
@@ -92,6 +100,7 @@
92100
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
93101
import com.oracle.graal.python.nodes.object.GetClassNode;
94102
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.GetCaughtExceptionNode;
103+
import com.oracle.graal.python.runtime.PosixSupportLibrary;
95104
import com.oracle.graal.python.runtime.PythonContext;
96105
import com.oracle.graal.python.runtime.PythonOptions;
97106
import com.oracle.graal.python.runtime.exception.PException;
@@ -113,6 +122,7 @@
113122
import com.oracle.truffle.api.frame.VirtualFrame;
114123
import com.oracle.truffle.api.nodes.Node;
115124
import com.oracle.truffle.api.profiles.ConditionProfile;
125+
import java.util.Map;
116126

117127
@CoreFunctions(defineModule = "sys", isEager = true)
118128
public class SysModuleBuiltins extends PythonBuiltins {
@@ -398,11 +408,10 @@ public void initialize(Python3Core core) {
398408
super.initialize(core);
399409

400410
// we need these during core initialization, they are re-set in postInitialize
401-
postInitialize(core);
411+
postInitialize0(core);
402412
}
403413

404-
@Override
405-
public void postInitialize(Python3Core core) {
414+
public void postInitialize0(Python3Core core) {
406415
super.postInitialize(core);
407416
PythonModule sys = core.lookupBuiltinModule("sys");
408417
PythonContext context = core.getContext();
@@ -488,6 +497,74 @@ public void postInitialize(Python3Core core) {
488497
));
489498
}
490499

500+
@Override
501+
public void postInitialize(Python3Core core) {
502+
postInitialize0(core);
503+
initStd(core);
504+
}
505+
506+
@TruffleBoundary
507+
public void initStd(Python3Core core) {
508+
TextIOWrapperInitNode textIOWrapperInitNode = TextIOWrapperInitNodeGen.getUncached();
509+
PythonObjectFactory factory = PythonObjectFactory.getUncached();
510+
511+
// wrap std in/out/err
512+
GraalPythonModuleBuiltins gp = (GraalPythonModuleBuiltins) core.lookupBuiltinModule("__graalpython__").getBuiltins();
513+
String stdioEncoding = gp.getStdIOEncoding();
514+
String stdioError = gp.getStdIOError();
515+
Object posixSupport = core.getContext().getPosixSupport();
516+
PosixSupportLibrary posixLib = PosixSupportLibrary.getUncached();
517+
PythonModule sysModule = core.lookupBuiltinModule("sys");
518+
519+
PBuffered reader = factory.createBufferedReader(PythonBuiltinClassType.PBufferedReader);
520+
BufferedReaderBuiltins.BufferedReaderInit.internalInit(reader, (PFileIO) get(builtinConstants, "stdin"), BufferedReaderBuiltins.DEFAULT_BUFFER_SIZE, factory, posixSupport,
521+
posixLib);
522+
setWrapper("stdin", "__stdin__", "r", stdioEncoding, stdioError, reader, sysModule, textIOWrapperInitNode, core.factory());
523+
524+
PBuffered writer = factory.createBufferedWriter(PythonBuiltinClassType.PBufferedWriter);
525+
BufferedWriterBuiltins.BufferedWriterInit.internalInit(writer, (PFileIO) get(builtinConstants, "stdout"), BufferedReaderBuiltins.DEFAULT_BUFFER_SIZE, factory, posixSupport,
526+
posixLib);
527+
PTextIO stdout = setWrapper("stdout", "__stdout__", "w", stdioEncoding, stdioError, writer, sysModule, textIOWrapperInitNode, core.factory());
528+
529+
writer = factory.createBufferedWriter(PythonBuiltinClassType.PBufferedWriter);
530+
BufferedWriterBuiltins.BufferedWriterInit.internalInit(writer, (PFileIO) get(builtinConstants, "stderr"), BufferedReaderBuiltins.DEFAULT_BUFFER_SIZE, factory, posixSupport,
531+
posixLib);
532+
PTextIO stderr = setWrapper("stderr", "__stderr__", "w", stdioEncoding, "backslashreplace", writer, sysModule, textIOWrapperInitNode, core.factory());
533+
534+
// register atexit close std out/err
535+
core.getContext().registerAtexitHook((ctx) -> {
536+
callClose(stdout);
537+
callClose(stderr);
538+
});
539+
}
540+
541+
private static Object get(Map<Object, Object> builtinConstants, Object key) {
542+
return builtinConstants.get(key);
543+
}
544+
545+
private static PTextIO setWrapper(String name, String specialName, String mode, String encoding, String error, PBuffered buffered, PythonModule sysModule,
546+
TextIOWrapperInitNode textIOWrapperInitNode, PythonObjectFactory factory) {
547+
PTextIO textIOWrapper = factory.createTextIO(PythonBuiltinClassType.PTextIOWrapper);
548+
textIOWrapperInitNode.execute(null, textIOWrapper, buffered, encoding, error, PNone.NONE, true, true);
549+
550+
setAttribute(textIOWrapper, "mode", mode);
551+
setAttribute(sysModule, name, textIOWrapper);
552+
setAttribute(sysModule, specialName, textIOWrapper);
553+
554+
return textIOWrapper;
555+
}
556+
557+
private static void setAttribute(PythonObject obj, String key, Object value) {
558+
obj.setAttribute(key, value);
559+
}
560+
561+
private static void callClose(Object obj) {
562+
try {
563+
PyObjectCallMethodObjArgs.getUncached().execute(null, obj, "close");
564+
} catch (PException e) {
565+
}
566+
}
567+
491568
@Builtin(name = "exc_info", needsFrame = true)
492569
@GenerateNodeFactory
493570
public abstract static class ExcInfoNode extends PythonBuiltinNode {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/AbstractBufferedIOBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
abstract class AbstractBufferedIOBuiltins extends PythonBuiltins {
7676

77-
protected static final int DEFAULT_BUFFER_SIZE = IOModuleBuiltins.DEFAULT_BUFFER_SIZE;
77+
public static final int DEFAULT_BUFFER_SIZE = IOModuleBuiltins.DEFAULT_BUFFER_SIZE;
7878

7979
public abstract static class BufferedInitNode extends PNodeWithRaise {
8080

graalpython/lib-graalpython/final_patches.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@
4040
import _imp
4141
import sys
4242

43+
# - make sure encodings gets frozen in scope of _imp.cache_all_file_modules
44+
# - at this point during context startup, sys.path isn't initialized,
45+
# so we need to set it up
46+
sys.path.append(__graalpython__.stdlib_home)
47+
try:
48+
import encodings
49+
finally:
50+
assert len(sys.path) == 1
51+
sys.path.pop()
52+
4353
_imp.cache_all_file_modules()
4454

4555
from importlib._bootstrap import BuiltinImporter

graalpython/lib-graalpython/sys_post_init.py

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

0 commit comments

Comments
 (0)