Patchwork [1,of,3] hook: do not redirect stdout/err/in to ui while running in-process hooks (BC)

mail settings
Submitter Yuya Nishihara
Date Nov. 10, 2016, 2:29 p.m.
Message ID <2d667ad51c1f2daa8ea5.1478788157@mimosa>
Download mbox | patch
Permalink /patch/17446/
State Accepted
Headers show


Yuya Nishihara - Nov. 10, 2016, 2:29 p.m.
# HG changeset patch
# User Yuya Nishihara <>
# Date 1476970799 -32400
#      Thu Oct 20 22:39:59 2016 +0900
# Node ID 2d667ad51c1f2daa8ea5365709365d951f610aaa
# Parent  954002426f7850c92b359e6f52ce1217ac33c11e
hook: do not redirect stdout/err/in to ui while running in-process hooks (BC)

It was introduced by a59058fd074a to address command-server issues. After
that, I've made a complete fix by 69f86b937035, so we don't need to replace
sys.stdio objects to protect the IPC channels.

This change means we no longer see data written to sys.stdout/err by an
in-process hook on command server. I think that's okay because the canonical
way is to use ui functions and in-process hooks should respect the Mercurial

This will help Python 3 porting, where sys.stdout is TextIO but ui.fout is


diff --git a/mercurial/ b/mercurial/
--- a/mercurial/
+++ b/mercurial/
@@ -90,12 +90,6 @@  def _pythonhook(ui, repo, name, hname, f
     starttime = time.time()
-        # redirect IO descriptors to the ui descriptors so hooks
-        # that write directly to these don't mess up the command
-        # protocol when running through the command server
-        old = sys.stdout, sys.stderr, sys.stdin
-        sys.stdout, sys.stderr, sys.stdin = ui.fout, ui.ferr, ui.fin
         r = obj(ui=ui, repo=repo, hooktype=name, **args)
     except Exception as exc:
         if isinstance(exc, error.Abort):
@@ -111,7 +105,6 @@  def _pythonhook(ui, repo, name, hname, f
         return True, True
-        sys.stdout, sys.stderr, sys.stdin = old
         duration = time.time() - starttime
         ui.log('pythonhook', 'pythonhook-%s: %s finished in %0.2f seconds\n',
                name, funcname, duration)
diff --git a/tests/test-commandserver.t b/tests/test-commandserver.t
--- a/tests/test-commandserver.t
+++ b/tests/test-commandserver.t
@@ -236,8 +236,6 @@  check that local configs for the cached 
   ...                         'id'],
   ...                input=stringio('some input'))
   *** runcommand --config hooks.pre-identify=python:hook.hook id
-  hook talking
-  now try to read something: 'some input'
   eff892de26ec tip
   $ rm*