comparison mercurial/hook.py @ 14889:a59058fd074a stable

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.
author Idan Kamara <idankk86@gmail.com>
date Sat, 09 Jul 2011 19:06:59 +0300
parents ac70f8d5987c
children 58f97dcbd550
comparison
equal deleted inserted replaced
14884:c0ccd70df52c 14889:a59058fd074a
63 if not hasattr(obj, '__call__'): 63 if not hasattr(obj, '__call__'):
64 raise util.Abort(_('%s hook is invalid ' 64 raise util.Abort(_('%s hook is invalid '
65 '("%s" is not callable)') % 65 '("%s" is not callable)') %
66 (hname, funcname)) 66 (hname, funcname))
67 try: 67 try:
68 # redirect IO descriptors the the ui descriptors so hooks that write
69 # directly to these don't mess the command protocol when running through
70 # the command server
71 old = sys.stdout, sys.stderr, sys.stdin
72 sys.stdout, sys.stderr, sys.stdin = ui.fout, ui.ferr, ui.fin
73
68 r = obj(ui=ui, repo=repo, hooktype=name, **args) 74 r = obj(ui=ui, repo=repo, hooktype=name, **args)
69 except KeyboardInterrupt: 75 except KeyboardInterrupt:
70 raise 76 raise
71 except Exception, exc: 77 except Exception, exc:
72 if isinstance(exc, util.Abort): 78 if isinstance(exc, util.Abort):
77 '%s\n') % (hname, exc)) 83 '%s\n') % (hname, exc))
78 if throw: 84 if throw:
79 raise 85 raise
80 ui.traceback() 86 ui.traceback()
81 return True 87 return True
88 finally:
89 sys.stdout, sys.stderr, sys.stdin = old
82 if r: 90 if r:
83 if throw: 91 if throw:
84 raise util.Abort(_('%s hook failed') % hname) 92 raise util.Abort(_('%s hook failed') % hname)
85 ui.warn(_('warning: %s hook failed\n') % hname) 93 ui.warn(_('warning: %s hook failed\n') % hname)
86 return r 94 return r