changeset 9658:852b1f3032d2

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.
author Sune Foldager <cryo@cyanite.org>
date Wed, 28 Oct 2009 21:35:57 +0100
parents 96c803e9018f
children f53c549237ca
files mercurial/hook.py
diffstat 1 files changed, 9 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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