[PATCH] hook: only redirect stdout if it and stderr are valid files

Sune Foldager cryo at cyanite.org
Wed Oct 28 20:41:38 UTC 2009


# HG changeset patch
# User Sune Foldager <cryo at cyanite.org>
# Date 1256762157 -3600
# Node ID d83f85808704ed9a589e8428daf55c73ace33067
# Parent  2ae3758526d873589ab28bb704a2e106df45c773
hook: only redirect stdout if it and stderr are valid files

When using hgwebdir with WSGI via the IIS ISAPI-WSGI extension, both
stdout and stderr filenos are set to -2, which makes the os.dup call
in hook.py fail.

diff --git a/mercurial/hook.py b/mercurial/hook.py
--- a/mercurial/hook.py
+++ b/mercurial/hook.py
@@ -106,10 +106,14 @@
 def hook(ui, repo, name, throw=False, **args):
     r = False
 
+    oldstdout = -1
     if _redirect:
-        # temporarily redirect stdout to stderr
-        oldstdout = os.dup(sys.__stdout__.fileno())
-        os.dup2(sys.__stderr__.fileno(), sys.__stdout__.fileno())
+        stdoutno = sys.__stdout__.fileno()
+        stderrno = sys.__stderr__.fileno()
+        # temporarily redirect stdout to stderr, if possible
+        if stdoutno >= 0 and stderrno >= 0:
+            oldstdout = os.dup(stdoutno)
+            os.dup2(stderrno, stdoutno)
 
     try:
         for hname, cmd in ui.configitems('hooks'):
@@ -128,8 +132,8 @@
             else:
                 r = _exthook(ui, repo, hname, cmd, args, throw) or r
     finally:
-        if _redirect:
-            os.dup2(oldstdout, sys.__stdout__.fileno())
+        if _redirect and oldstdout >= 0:
+            os.dup2(oldstdout, stdoutno)
             os.close(oldstdout)
 
     return r



More information about the Mercurial-devel mailing list