]> granicus.if.org Git - python/commitdiff
bpo-28326: Fix multiprocessing.Process when stdout and/or stderr is closed or None...
authorAntoine Pitrou <pitrou@free.fr>
Sun, 22 Oct 2017 09:40:31 +0000 (11:40 +0200)
committerGitHub <noreply@github.com>
Sun, 22 Oct 2017 09:40:31 +0000 (11:40 +0200)
Lib/multiprocessing/popen_fork.py
Lib/test/_test_multiprocessing.py
Misc/NEWS.d/next/Library/2017-10-22-11-06-02.bpo-28326.rxh7L4.rst [new file with mode: 0644]

index cbdbfa79cf3c0dbdd359d0859d119c0be76f0ab8..b0fc01396eb24cfe285fc39dce162fb93020d55a 100644 (file)
@@ -14,8 +14,14 @@ class Popen(object):
     method = 'fork'
 
     def __init__(self, process_obj):
-        sys.stdout.flush()
-        sys.stderr.flush()
+        try:
+            sys.stdout.flush()
+        except (AttributeError, ValueError):
+            pass
+        try:
+            sys.stderr.flush()
+        except (AttributeError, ValueError):
+            pass
         self.returncode = None
         self.finalizer = None
         self._launch(process_obj)
index 8e004e21a4fd5ad7841bfc2fe2580cfe70e3c4e3..69c0bd892c8686f08fd23905841ada1d59b78f99 100644 (file)
@@ -582,6 +582,27 @@ class _TestProcess(BaseTestCase):
         proc.join()
         self.assertTrue(evt.is_set())
 
+    @classmethod
+    def _test_error_on_stdio_flush(self, evt):
+        evt.set()
+
+    def test_error_on_stdio_flush(self):
+        streams = [io.StringIO(), None]
+        streams[0].close()
+        for stream_name in ('stdout', 'stderr'):
+            for stream in streams:
+                old_stream = getattr(sys, stream_name)
+                setattr(sys, stream_name, stream)
+                try:
+                    evt = self.Event()
+                    proc = self.Process(target=self._test_error_on_stdio_flush,
+                                        args=(evt,))
+                    proc.start()
+                    proc.join()
+                    self.assertTrue(evt.is_set())
+                finally:
+                    setattr(sys, stream_name, old_stream)
+
 
 #
 #
diff --git a/Misc/NEWS.d/next/Library/2017-10-22-11-06-02.bpo-28326.rxh7L4.rst b/Misc/NEWS.d/next/Library/2017-10-22-11-06-02.bpo-28326.rxh7L4.rst
new file mode 100644 (file)
index 0000000..bcf43bc
--- /dev/null
@@ -0,0 +1 @@
+Fix multiprocessing.Process when stdout and/or stderr is closed or None.