diff mercurial/utils/procutil.py @ 37222:7f78de1c93aa

procutil: redirect ui.fout to stderr while stdio is protected The new behavior seems slightly nicer as we can at least read the output. And this is similar to what the sshserver is doing, so we can probably reuse protectstdio() instead of the weird hook.redirect(True) hack.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 25 Mar 2018 12:15:33 +0900
parents ac71cbad5da3
children 90c5ca718781
line wrap: on
line diff
--- a/mercurial/utils/procutil.py	Sun Mar 25 12:07:18 2018 +0900
+++ b/mercurial/utils/procutil.py	Sun Mar 25 12:15:33 2018 +0900
@@ -213,25 +213,27 @@
     return _testfileno(f, sys.__stdout__)
 
 def protectstdio(uin, uout):
-    """Duplicate streams and redirect original to null if (uin, uout) are
-    stdio
+    """Duplicate streams and redirect original if (uin, uout) are stdio
+
+    If uin is stdin, it's redirected to /dev/null. If uout is stdout, it's
+    redirected to stderr so the output is still readable.
 
     Returns (fin, fout) which point to the original (uin, uout) fds, but
     may be copy of (uin, uout). The returned streams can be considered
     "owned" in that print(), exec(), etc. never reach to them.
     """
     uout.flush()
-    nullfd = os.open(os.devnull, os.O_RDWR)
     fin, fout = uin, uout
     if uin is stdin:
         newfd = os.dup(uin.fileno())
+        nullfd = os.open(os.devnull, os.O_RDONLY)
         os.dup2(nullfd, uin.fileno())
+        os.close(nullfd)
         fin = os.fdopen(newfd, r'rb')
     if uout is stdout:
         newfd = os.dup(uout.fileno())
-        os.dup2(nullfd, uout.fileno())
+        os.dup2(stderr.fileno(), uout.fileno())
         fout = os.fdopen(newfd, r'wb')
-    os.close(nullfd)
     return fin, fout
 
 def restorestdio(uin, uout, fin, fout):