Mercurial > public > mercurial-scm > hg-stable
diff mercurial/commandserver.py @ 37126:0216232f21ab
procutil: move protectio/restoreio from commandserver
Some variants of this will be useful for stdio-based servers such as
sshserver.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 25 Mar 2018 11:40:30 +0900 |
parents | 4f742c2cb837 |
children | 6715e8035b4f |
line wrap: on
line diff
--- a/mercurial/commandserver.py Sun Mar 25 11:30:59 2018 +0900 +++ b/mercurial/commandserver.py Sun Mar 25 11:40:30 2018 +0900 @@ -306,35 +306,6 @@ return 0 -def _protectio(uin, uout): - """Duplicate streams and redirect original to null if (uin, uout) are - stdio - - 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() - newfiles = [] - nullfd = os.open(os.devnull, os.O_RDWR) - for f, sysf, mode in [(uin, procutil.stdin, r'rb'), - (uout, procutil.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) - os.close(nullfd) - return tuple(newfiles) - -def _restoreio(uin, uout, fin, fout): - """Restore (uin, uout) streams from possibly duplicated (fin, fout)""" - uout.flush() - for f, uif in [(fin, uin), (fout, uout)]: - if f is not uif: - os.dup2(f.fileno(), uif.fileno()) - f.close() - class pipeservice(object): def __init__(self, ui, repo, opts): self.ui = ui @@ -347,13 +318,13 @@ ui = self.ui # redirect stdio to null device so that broken extensions or in-process # hooks will never cause corruption of channel protocol. - fin, fout = _protectio(ui.fin, ui.fout) + fin, fout = procutil.protectstdio(ui.fin, ui.fout) try: sv = server(ui, self.repo, fin, fout) return sv.serve() finally: sv.cleanup() - _restoreio(ui.fin, ui.fout, fin, fout) + procutil.restorestdio(ui.fin, ui.fout, fin, fout) def _initworkerprocess(): # use a different process group from the master process, in order to: