[PATCH STABLE] hooks: redirect stdout/err/in to the ui descriptors when calling python hooks

Idan Kamara idankk86 at gmail.com
Sat Jul 16 06:47:59 CDT 2011


# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1310227619 -10800
# Node ID 926c90623182540929d56ba2500f88f42e8328e5
# Parent  50b67d5c4073dc37d0fe549b178bc74c8b9c51f0
hooks: redirect stdout/err/in to the ui descriptors when calling python hooks

We need to make sure that python hooks I/O goes through the ui descriptors so
it doesn't mess the command server protocol.

diff -r 50b67d5c4073 -r 926c90623182 mercurial/hook.py
--- a/mercurial/hook.py	Fri Jul 15 14:27:16 2011 -0500
+++ b/mercurial/hook.py	Sat Jul 09 19:06:59 2011 +0300
@@ -65,6 +65,12 @@
                                '("%s" is not callable)') %
                              (hname, funcname))
     try:
+        # redirect IO descriptors the the ui descriptors so hooks that write
+        # directly to these don't mess 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 KeyboardInterrupt:
         raise
@@ -79,6 +85,8 @@
             raise
         ui.traceback()
         return True
+    finally:
+        sys.stdout, sys.stderr, sys.stdin = old
     if r:
         if throw:
             raise util.Abort(_('%s hook failed') % hname)
diff -r 50b67d5c4073 -r 926c90623182 tests/test-commandserver.py
--- a/tests/test-commandserver.py	Fri Jul 15 14:27:16 2011 -0500
+++ b/tests/test-commandserver.py	Sat Jul 09 19:06:59 2011 +0300
@@ -144,6 +144,16 @@
     runcommand(server, ['-R', 'foo', 'showconfig'])
     shutil.rmtree('foo')
 
+def hook(**args):
+    print 'hook talking'
+    print 'now try to read something: %r' % sys.stdin.read()
+
+def hookoutput(server):
+    readchannel(server)
+    runcommand(server, ['--config',
+                        'hooks.pre-identify=python:test-commandserver.hook', 'id'],
+               input=cStringIO.StringIO('some input'))
+
 if __name__ == '__main__':
     os.system('hg init')
 
@@ -158,3 +168,4 @@
     hgrc.write('[ui]\nfoo=bar\n')
     hgrc.close()
     check(localhgrc)
+    check(hookoutput)
diff -r 50b67d5c4073 -r 926c90623182 tests/test-commandserver.py.out
--- a/tests/test-commandserver.py.out	Fri Jul 15 14:27:16 2011 -0500
+++ b/tests/test-commandserver.py.out	Sat Jul 09 19:06:59 2011 +0300
@@ -49,3 +49,6 @@
 defaults.commit=-d "0 0"
 defaults.tag=-d "0 0"
 ui.slash=True
+hook talking
+now try to read something: 'some input'
+eff892de26ec tip


More information about the Mercurial-devel mailing list