Patchwork [1,of,2,RESEND] procutil: unroll uin/uout loop in protectstdio()

login
register
mail settings
Submitter Yuya Nishihara
Date March 31, 2018, 1:48 a.m.
Message ID <6fdab0e7824cd95ea5ed.1522460926@mimosa>
Download mbox | patch
Permalink /patch/30036/
State Accepted
Headers show

Comments

Yuya Nishihara - March 31, 2018, 1:48 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521947238 -32400
#      Sun Mar 25 12:07:18 2018 +0900
# Node ID 6fdab0e7824cd95ea5edd4511d5063823ba423d5
# Parent  40be6a8728a046b3673a517af2a3e15574457728
procutil: unroll uin/uout loop in protectstdio()

I'll change uout to be redirected to stderr.

Patch

diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py
--- a/mercurial/utils/procutil.py
+++ b/mercurial/utils/procutil.py
@@ -221,17 +221,18 @@  def protectstdio(uin, uout):
     "owned" in that print(), exec(), etc. never reach to them.
     """
     uout.flush()
-    newfiles = []
     nullfd = os.open(os.devnull, os.O_RDWR)
-    for f, sysf, mode in [(uin, stdin, r'rb'),
-                          (uout, stdout, r'wb')]:
-        if f is sysf:
-            newfd = os.dup(f.fileno())
-            os.dup2(nullfd, f.fileno())
-            f = os.fdopen(newfd, mode)
-        newfiles.append(f)
+    fin, fout = uin, uout
+    if uin is stdin:
+        newfd = os.dup(uin.fileno())
+        os.dup2(nullfd, uin.fileno())
+        fin = os.fdopen(newfd, r'rb')
+    if uout is stdout:
+        newfd = os.dup(uout.fileno())
+        os.dup2(nullfd, uout.fileno())
+        fout = os.fdopen(newfd, r'wb')
     os.close(nullfd)
-    return tuple(newfiles)
+    return fin, fout
 
 def restorestdio(uin, uout, fin, fout):
     """Restore (uin, uout) streams from possibly duplicated (fin, fout)"""