Mercurial > public > mercurial-scm > hg
comparison mercurial/hook.py @ 30363:a1259e502bdf
hook: do not redirect stdout/err/in to ui while running in-process hooks (BC)
It was introduced by a59058fd074a to address command-server issues. After
that, I've made a complete fix by 69f86b937035, so we don't need to replace
sys.stdio objects to protect the IPC channels.
This change means we no longer see data written to sys.stdout/err by an
in-process hook on command server. I think that's okay because the canonical
way is to use ui functions and in-process hooks should respect the Mercurial
API.
This will help Python 3 porting, where sys.stdout is TextIO but ui.fout is
BytesIO.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Thu, 20 Oct 2016 22:39:59 +0900 |
parents | ea1fec3e9aba |
children | ad56204f733e |
comparison
equal
deleted
inserted
replaced
30362:3c6893ba2d36 | 30363:a1259e502bdf |
---|---|
88 | 88 |
89 ui.note(_("calling hook %s: %s\n") % (hname, funcname)) | 89 ui.note(_("calling hook %s: %s\n") % (hname, funcname)) |
90 starttime = time.time() | 90 starttime = time.time() |
91 | 91 |
92 try: | 92 try: |
93 # redirect IO descriptors to the ui descriptors so hooks | |
94 # that write directly to these don't mess up the command | |
95 # protocol when running through the command server | |
96 old = sys.stdout, sys.stderr, sys.stdin | |
97 sys.stdout, sys.stderr, sys.stdin = ui.fout, ui.ferr, ui.fin | |
98 | |
99 r = obj(ui=ui, repo=repo, hooktype=name, **args) | 93 r = obj(ui=ui, repo=repo, hooktype=name, **args) |
100 except Exception as exc: | 94 except Exception as exc: |
101 if isinstance(exc, error.Abort): | 95 if isinstance(exc, error.Abort): |
102 ui.warn(_('error: %s hook failed: %s\n') % | 96 ui.warn(_('error: %s hook failed: %s\n') % |
103 (hname, exc.args[0])) | 97 (hname, exc.args[0])) |
109 if not ui.tracebackflag: | 103 if not ui.tracebackflag: |
110 ui.warn(_('(run with --traceback for stack trace)\n')) | 104 ui.warn(_('(run with --traceback for stack trace)\n')) |
111 ui.traceback() | 105 ui.traceback() |
112 return True, True | 106 return True, True |
113 finally: | 107 finally: |
114 sys.stdout, sys.stderr, sys.stdin = old | |
115 duration = time.time() - starttime | 108 duration = time.time() - starttime |
116 ui.log('pythonhook', 'pythonhook-%s: %s finished in %0.2f seconds\n', | 109 ui.log('pythonhook', 'pythonhook-%s: %s finished in %0.2f seconds\n', |
117 name, funcname, duration) | 110 name, funcname, duration) |
118 if r: | 111 if r: |
119 if throw: | 112 if throw: |