]> granicus.if.org Git - python/commitdiff
Issue #12493: subprocess: communicate() handles EINTR
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 5 Jul 2011 12:08:01 +0000 (14:08 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 5 Jul 2011 12:08:01 +0000 (14:08 +0200)
subprocess.Popen.communicate() now also handles EINTR errors if the process has
only one pipe.

Lib/subprocess.py
Lib/test/test_subprocess.py
Misc/NEWS

index bdf85fc108f3e71ee7157f1e80f79a4964b7aa01..ce08f83939cb51b39dcef8c20771685ab21f7841 100644 (file)
@@ -476,7 +476,7 @@ def _eintr_retry_call(func, *args):
     while True:
         try:
             return func(*args)
-        except OSError, e:
+        except (OSError, IOError) as e:
             if e.errno == errno.EINTR:
                 continue
             raise
@@ -743,10 +743,10 @@ class Popen(object):
                             raise
                 self.stdin.close()
             elif self.stdout:
-                stdout = self.stdout.read()
+                stdout = _eintr_retry_call(self.stdout.read)
                 self.stdout.close()
             elif self.stderr:
-                stderr = self.stderr.read()
+                stderr = _eintr_retry_call(self.stderr.read)
                 self.stderr.close()
             self.wait()
             return (stdout, stderr)
index a93602a7f84fed828fd6bef4cc3459303762bd91..a17f2f7640fe551f12224bdc7764754d95859f9a 100644 (file)
@@ -664,6 +664,22 @@ class _SuppressCoreFiles(object):
         except (ImportError, ValueError, resource.error):
             pass
 
+    def test_communicate_eintr(self):
+        # Issue #12493: communicate() should handle EINTR
+        def handler(signum, frame):
+            pass
+        old_handler = signal.signal(signal.SIGALRM, handler)
+        self.addCleanup(signal.signal, signal.SIGALRM, old_handler)
+
+        # the process is running for 2 seconds
+        args = [sys.executable, "-c", 'import time; time.sleep(2)']
+        for stream in ('stdout', 'stderr'):
+            kw = {stream: subprocess.PIPE}
+            with subprocess.Popen(args, **kw) as process:
+                signal.alarm(1)
+                # communicate() will be interrupted by SIGALRM
+                process.communicate()
+
 
 @unittest.skipIf(mswindows, "POSIX specific tests")
 class POSIXProcessTestCase(BaseTestCase):
index 38dff5bece805425997ac9a2cc6f95598449528a..d062d10223bf092e8c7313620fd5113b8fb43047 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #12493: subprocess: Popen.communicate() now also handles EINTR errors
+  if the process has only one pipe.
+
 - Issue #12467: warnings: fix a race condition if a warning is emitted at
   shutdown, if globals()['__file__'] is None.