hook: only redirect stdout if it and stderr are valid files
authorSune Foldager <cryo@cyanite.org>
Wed, 28 Oct 2009 21:35:57 +0100
changeset 9658 852b1f3032d2
parent 9657 96c803e9018f
child 9659 f53c549237ca
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.
mercurial/hook.py
--- a/mercurial/hook.py	Wed Oct 28 13:17:03 2009 -0500
+++ b/mercurial/hook.py	Wed Oct 28 21:35:57 2009 +0100
@@ -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