Mercurial > hg
changeset 37124:6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
The code looks slightly cleaner since it was pretty easy to pass arguments
in wrong order.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 25 Mar 2018 11:58:05 +0900 |
parents | 0216232f21ab |
children | 6f570c501e3e |
files | mercurial/commandserver.py mercurial/utils/procutil.py |
diffstat | 2 files changed, 16 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/commandserver.py Sun Mar 25 11:40:30 2018 +0900 +++ b/mercurial/commandserver.py Sun Mar 25 11:58:05 2018 +0900 @@ -318,13 +318,12 @@ 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 = procutil.protectstdio(ui.fin, ui.fout) - try: - sv = server(ui, self.repo, fin, fout) - return sv.serve() - finally: - sv.cleanup() - procutil.restorestdio(ui.fin, ui.fout, fin, fout) + with procutil.protectedstdio(ui.fin, ui.fout) as (fin, fout): + try: + sv = server(ui, self.repo, fin, fout) + return sv.serve() + finally: + sv.cleanup() def _initworkerprocess(): # use a different process group from the master process, in order to:
--- a/mercurial/utils/procutil.py Sun Mar 25 11:40:30 2018 +0900 +++ b/mercurial/utils/procutil.py Sun Mar 25 11:58:05 2018 +0900 @@ -9,6 +9,7 @@ from __future__ import absolute_import +import contextlib import imp import io import os @@ -240,6 +241,15 @@ os.dup2(f.fileno(), uif.fileno()) f.close() +@contextlib.contextmanager +def protectedstdio(uin, uout): + """Run code block with protected standard streams""" + fin, fout = protectstdio(uin, uout) + try: + yield fin, fout + finally: + restorestdio(uin, uout, fin, fout) + def shellenviron(environ=None): """return environ with optional override, useful for shelling out""" def py2shell(val):