Patchwork [1,of,2] ui: move protectedstdio() context manager from procutil

login
register
mail settings
Submitter Yuya Nishihara
Date Jan. 12, 2019, 10:50 a.m.
Message ID <c5a71f1cdd3278159500.1547290203@mimosa>
Download mbox | patch
Permalink /patch/37690/
State Accepted
Headers show

Comments

Yuya Nishihara - Jan. 12, 2019, 10:50 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1537964953 -32400
#      Wed Sep 26 21:29:13 2018 +0900
# Node ID c5a71f1cdd3278159500360435863ee01c842cf4
# Parent  ead5fb5b2f2908c77aee99cde8b298e407a6efc8
ui: move protectedstdio() context manager from procutil

This is a follow-up series for 23a00bc90a3c, "chgserver: do not send system()
back to client if stdio redirected." The function is renamed using ui terms.

Patch

diff --git a/mercurial/commandserver.py b/mercurial/commandserver.py
--- a/mercurial/commandserver.py
+++ b/mercurial/commandserver.py
@@ -399,7 +399,7 @@  class pipeservice(object):
         ui = self.ui
         # redirect stdio to null device so that broken extensions or in-process
         # hooks will never cause corruption of channel protocol.
-        with procutil.protectedstdio(ui.fin, ui.fout) as (fin, fout):
+        with ui.protectedfinout() as (fin, fout):
             sv = server(ui, self.repo, fin, fout)
             try:
                 return sv.serve()
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1080,6 +1080,15 @@  class ui(object):
             return False
         return procutil.isatty(fh)
 
+    @contextlib.contextmanager
+    def protectedfinout(self):
+        """Run code block with protected standard streams"""
+        fin, fout = procutil.protectstdio(self._fin, self._fout)
+        try:
+            yield fin, fout
+        finally:
+            procutil.restorestdio(self._fin, self._fout, fin, fout)
+
     def disablepager(self):
         self._disablepager = True
 
diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py
--- a/mercurial/utils/procutil.py
+++ b/mercurial/utils/procutil.py
@@ -299,15 +299,6 @@  def restorestdio(uin, uout, fin, fout):
             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):