hooks: delay I/O redirection until we actually run a hook (issue3711) stable
authorMatt Mackall <mpm@selenic.com>
Mon, 26 Nov 2012 16:14:22 -0600
branchstable
changeset 17963 6180dcb29ec5
parent 17962 4c29668ca316
child 17964 2c63896783e3
hooks: delay I/O redirection until we actually run a hook (issue3711) We were attempting to redirect I/O even if no hook was actually getting called. This defers redirection until we've found something to do.
mercurial/hook.py
--- a/mercurial/hook.py	Mon Nov 26 15:42:52 2012 -0600
+++ b/mercurial/hook.py	Mon Nov 26 16:14:22 2012 -0600
@@ -142,25 +142,26 @@
         return False
 
     r = False
-
     oldstdout = -1
-    if _redirect:
-        try:
-            stdoutno = sys.__stdout__.fileno()
-            stderrno = sys.__stderr__.fileno()
-            # temporarily redirect stdout to stderr, if possible
-            if stdoutno >= 0 and stderrno >= 0:
-                sys.__stdout__.flush()
-                oldstdout = os.dup(stdoutno)
-                os.dup2(stderrno, stdoutno)
-        except AttributeError:
-            # __stdout__/__stderr__ doesn't have fileno(), it's not a real file
-            pass
 
     try:
         for hname, cmd in _allhooks(ui):
             if hname.split('.')[0] != name or not cmd:
                 continue
+
+            if oldstdout == -1 and _redirect:
+                try:
+                    stdoutno = sys.__stdout__.fileno()
+                    stderrno = sys.__stderr__.fileno()
+                    # temporarily redirect stdout to stderr, if possible
+                    if stdoutno >= 0 and stderrno >= 0:
+                        sys.__stdout__.flush()
+                        oldstdout = os.dup(stdoutno)
+                        os.dup2(stderrno, stdoutno)
+                except AttributeError:
+                    # __stdout__/__stderr__ has no fileno(), not a real file
+                    pass
+
             if util.safehasattr(cmd, '__call__'):
                 r = _pythonhook(ui, repo, name, hname, cmd, args, throw) or r
             elif cmd.startswith('python:'):