runtime/spawn_subprocess.py
changeset 3881 0b3ac94f494c
parent 3821 f7bc4e5832be
parent 3860 a1d7187b8402
--- a/runtime/spawn_subprocess.py	Sat Nov 25 00:18:05 2023 +0100
+++ b/runtime/spawn_subprocess.py	Thu Dec 07 22:41:32 2023 +0100
@@ -15,17 +15,24 @@
 fsencoding = sys.getfilesystemencoding()
 
 class Popen(object):
-    def __init__(self, args, stdin=None, stdout=None):
+    def __init__(self, args, stdin=None, stdout=None, stderr=None):
         self.returncode = None
         self.stdout = None
+        self.stderr = None
         self.stdin = None
-        # TODO: stderr
         file_actions = posix_spawn.FileActions()
         if stdout is not None:
             # child's stdout, child 2 parent pipe
+            c1pread, c1pwrite = os.pipe()
+            # attach child's stdout to writing en of c1p pipe
+            file_actions.add_dup2(c1pwrite, 1)
+            # close other end
+            file_actions.add_close(c1pread)
+        if stderr is not None:
+            # child's stderr, child 2 parent pipe
             c2pread, c2pwrite = os.pipe()
-            # attach child's stdout to writing en of c2p pipe
-            file_actions.add_dup2(c2pwrite, 1)
+            # attach child's stderr to writing en of c2p pipe
+            file_actions.add_dup2(c2pwrite, 2)
             # close other end
             file_actions.add_close(c2pread)
         if stdin is not None:
@@ -38,7 +45,10 @@
         args = [s.encode(fsencoding) for s in args if type(s)==str]
         self.pid = posix_spawn.posix_spawnp(args[0], args, file_actions=file_actions)
         if stdout is not None:
-            self.stdout = os.fdopen(c2pread)
+            self.stdout = os.fdopen(c1pread)
+            os.close(c1pwrite)
+        if stderr is not None:
+            self.stderr = os.fdopen(c2pread)
             os.close(c2pwrite)
         if stdin is not None:
             self.stdin = os.fdopen(p2cwrite, 'w')
@@ -52,29 +62,44 @@
         if self.stdin is not None:
             self.stdin.close()
             self.stdin = None
+
         if self.stdout is not None:
             stdoutdata = self.stdout.read()
         else:
             stdoutdata = ""
 
-        # TODO
-        stderrdata = ""
+        if self.stderr is not None:
+            stderrdata = self.stderr.read()
+        else:
+            stderrdata = ""
 
         self._wait()
+
         if self.stdout is not None:
             self.stdout.close()
             self.stdout = None
 
+        if self.stderr is not None:
+            self.stderr.close()
+            self.stderr = None
+
         return (stdoutdata, stderrdata)
 
     def wait(self):
         if self.stdin is not None:
             self.stdin.close()
             self.stdin = None
+
         self._wait()
+
         if self.stdout is not None:
             self.stdout.close()
             self.stdout = None
+
+        if self.stderr is not None:
+            self.stderr.close()
+            self.stderr = None
+
         return self.returncode
 
     def poll(self):
@@ -86,10 +111,15 @@
                 if self.stdin is not None:
                     self.stdin.close()
                     self.stdin = None
+
                 if self.stdout is not None:
                     self.stdout.close()
                     self.stdout = None
 
+                if self.stderr is not None:
+                    self.stderr.close()
+                    self.stderr = None
+
         return self.returncode
 
     def kill(self):
@@ -98,10 +128,15 @@
         if self.stdin is not None:
             self.stdin.close()
             self.stdin = None
+
         if self.stdout is not None:
             self.stdout.close()
             self.stdout = None
 
+        if self.stderr is not None:
+            self.stderr.close()
+            self.stderr = None
+
 
 def call(*args):
     cmd = []