comparison mercurial/utils/procutil.py @ 37219:ac71cbad5da3

procutil: unroll uin/uout loop in protectstdio() I'll change uout to be redirected to stderr.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 25 Mar 2018 12:07:18 +0900
parents 6715e8035b4f
children 7f78de1c93aa
comparison
equal deleted inserted replaced
37218:97ab6f2dc3c3 37219:ac71cbad5da3
219 Returns (fin, fout) which point to the original (uin, uout) fds, but 219 Returns (fin, fout) which point to the original (uin, uout) fds, but
220 may be copy of (uin, uout). The returned streams can be considered 220 may be copy of (uin, uout). The returned streams can be considered
221 "owned" in that print(), exec(), etc. never reach to them. 221 "owned" in that print(), exec(), etc. never reach to them.
222 """ 222 """
223 uout.flush() 223 uout.flush()
224 newfiles = []
225 nullfd = os.open(os.devnull, os.O_RDWR) 224 nullfd = os.open(os.devnull, os.O_RDWR)
226 for f, sysf, mode in [(uin, stdin, r'rb'), 225 fin, fout = uin, uout
227 (uout, stdout, r'wb')]: 226 if uin is stdin:
228 if f is sysf: 227 newfd = os.dup(uin.fileno())
229 newfd = os.dup(f.fileno()) 228 os.dup2(nullfd, uin.fileno())
230 os.dup2(nullfd, f.fileno()) 229 fin = os.fdopen(newfd, r'rb')
231 f = os.fdopen(newfd, mode) 230 if uout is stdout:
232 newfiles.append(f) 231 newfd = os.dup(uout.fileno())
232 os.dup2(nullfd, uout.fileno())
233 fout = os.fdopen(newfd, r'wb')
233 os.close(nullfd) 234 os.close(nullfd)
234 return tuple(newfiles) 235 return fin, fout
235 236
236 def restorestdio(uin, uout, fin, fout): 237 def restorestdio(uin, uout, fin, fout):
237 """Restore (uin, uout) streams from possibly duplicated (fin, fout)""" 238 """Restore (uin, uout) streams from possibly duplicated (fin, fout)"""
238 uout.flush() 239 uout.flush()
239 for f, uif in [(fin, uin), (fout, uout)]: 240 for f, uif in [(fin, uin), (fout, uout)]: