Patchwork util.system: avoid buffering of subprocess output when it is piped

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 14, 2014, 1:57 p.m.
Message ID <114069ffd21cb7b70513.1413295021@mimosa>
Download mbox | patch
Permalink /patch/6248/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 14, 2014, 1:57 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1409413094 -7200
#      Sat Aug 30 17:38:14 2014 +0200
# Node ID 114069ffd21cb7b70513975d2b4db0712169f500
# Parent  cc4be0f3d54ce2cdbfe6d6c8bc25de74d07b437c
util.system: avoid buffering of subprocess output when it is piped

util.system() copies subprocess' output through pipe if output file is not
stdout.  Because a file iterator has internal buffering, output won't be
flushed until enough data is available.  Therefore, it could easily miss
important messages such as "waiting for lock".
Matt Mackall - Oct. 17, 2014, 10:26 p.m.
On Tue, 2014-10-14 at 22:57 +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1409413094 -7200
> #      Sat Aug 30 17:38:14 2014 +0200
> # Node ID 114069ffd21cb7b70513975d2b4db0712169f500
> # Parent  cc4be0f3d54ce2cdbfe6d6c8bc25de74d07b437c
> util.system: avoid buffering of subprocess output when it is piped

Queued for default, thanks.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -543,7 +543,10 @@  def system(cmd, environ={}, cwd=None, on
             proc = subprocess.Popen(cmd, shell=True, close_fds=closefds,
                                     env=env, cwd=cwd, stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT)
-            for line in proc.stdout:
+            while True:
+                line = proc.stdout.readline()
+                if not line:
+                    break
                 out.write(line)
             proc.wait()
             rc = proc.returncode