]> granicus.if.org Git - python/commitdiff
[3.6] bpo-28326: Fix multiprocessing.Process when stdout and/or stderr is closed...
authorAntoine Pitrou <pitrou@free.fr>
Sun, 22 Oct 2017 10:27:13 +0000 (12:27 +0200)
committerGitHub <noreply@github.com>
Sun, 22 Oct 2017 10:27:13 +0000 (12:27 +0200)
* bpo-28326: Fix multiprocessing.Process when stdout and/or stderr is closed or None. (#4073)

(cherry picked from commit daeefd2e049b74340307481112a39f77de0f4769)

* [3.6] bpo-28326: Fix multiprocessing.Process when stdout and/or stderr is closed or None. (GH-4073).
(cherry picked from commit daeefd2e049b74340307481112a39f77de0f4769)

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 d2ebd7cfbe1e03de90be6075bbdf3d87066b3bf1..5d0fa569f12eaaa3e9789539fdea820229f90f8e 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._launch(process_obj)
 
index db30e6b9d22a0c04acbb0532b39418db5292411d..80c95d6bca6fb8d119ccde3a2d0aec42dcc9ab6c 100644 (file)
@@ -425,6 +425,27 @@ class _TestProcess(BaseTestCase):
         self.assertEqual(q.get(), 5)
         close_queue(q)
 
+    @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.