|
62 | 62 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
63 | 63 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
64 | 64 | 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; |
65 | 67 | import com.oracle.graal.python.builtins.modules.io.FileIOBuiltins;
|
| 68 | +import com.oracle.graal.python.builtins.modules.io.PBuffered; |
66 | 69 | 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; |
67 | 73 | import com.oracle.graal.python.builtins.objects.PNone;
|
68 | 74 | import com.oracle.graal.python.builtins.objects.exception.PBaseException;
|
69 | 75 | import com.oracle.graal.python.builtins.objects.frame.PFrame;
|
|
73 | 79 | import com.oracle.graal.python.builtins.objects.list.PList;
|
74 | 80 | import com.oracle.graal.python.builtins.objects.module.PythonModule;
|
75 | 81 | import com.oracle.graal.python.builtins.objects.namespace.PSimpleNamespace;
|
| 82 | +import com.oracle.graal.python.builtins.objects.object.PythonObject; |
76 | 83 | import com.oracle.graal.python.builtins.objects.str.PString;
|
77 | 84 | import com.oracle.graal.python.builtins.objects.str.StringNodes;
|
78 | 85 | import com.oracle.graal.python.builtins.objects.traceback.GetTracebackNode;
|
|
81 | 88 | import com.oracle.graal.python.builtins.objects.tuple.PTuple;
|
82 | 89 | import com.oracle.graal.python.builtins.objects.tuple.StructSequence;
|
83 | 90 | import com.oracle.graal.python.lib.PyNumberAsSizeNode;
|
| 91 | +import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs; |
84 | 92 | import com.oracle.graal.python.nodes.ErrorMessages;
|
85 | 93 | import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
|
86 | 94 | import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode.NoAttributeHandler;
|
|
92 | 100 | import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
|
93 | 101 | import com.oracle.graal.python.nodes.object.GetClassNode;
|
94 | 102 | import com.oracle.graal.python.nodes.util.ExceptionStateNodes.GetCaughtExceptionNode;
|
| 103 | +import com.oracle.graal.python.runtime.PosixSupportLibrary; |
95 | 104 | import com.oracle.graal.python.runtime.PythonContext;
|
96 | 105 | import com.oracle.graal.python.runtime.PythonOptions;
|
97 | 106 | import com.oracle.graal.python.runtime.exception.PException;
|
|
113 | 122 | import com.oracle.truffle.api.frame.VirtualFrame;
|
114 | 123 | import com.oracle.truffle.api.nodes.Node;
|
115 | 124 | import com.oracle.truffle.api.profiles.ConditionProfile;
|
| 125 | +import java.util.Map; |
116 | 126 |
|
117 | 127 | @CoreFunctions(defineModule = "sys", isEager = true)
|
118 | 128 | public class SysModuleBuiltins extends PythonBuiltins {
|
@@ -398,11 +408,10 @@ public void initialize(Python3Core core) {
|
398 | 408 | super.initialize(core);
|
399 | 409 |
|
400 | 410 | // we need these during core initialization, they are re-set in postInitialize
|
401 |
| - postInitialize(core); |
| 411 | + postInitialize0(core); |
402 | 412 | }
|
403 | 413 |
|
404 |
| - @Override |
405 |
| - public void postInitialize(Python3Core core) { |
| 414 | + public void postInitialize0(Python3Core core) { |
406 | 415 | super.postInitialize(core);
|
407 | 416 | PythonModule sys = core.lookupBuiltinModule("sys");
|
408 | 417 | PythonContext context = core.getContext();
|
@@ -488,6 +497,74 @@ public void postInitialize(Python3Core core) {
|
488 | 497 | ));
|
489 | 498 | }
|
490 | 499 |
|
| 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 | + |
491 | 568 | @Builtin(name = "exc_info", needsFrame = true)
|
492 | 569 | @GenerateNodeFactory
|
493 | 570 | public abstract static class ExcInfoNode extends PythonBuiltinNode {
|
|
0 commit comments