Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/hook.py @ 30375:5564fcd031df
hook: lower inflated use of sys.__stdout__ and __stderr__
They were introduced at 9f76df0edb7d, where sys.stdout could be replaced by
sys.stderr. After that, we've changed the way of stdout redirection by
afccc64eea73, so we no longer need to reference the original __stdout__ and
__stderr__ objects.
Let's move away from using __std*__ objects so we can simply wrap sys.std*
objects for Python 3 porting.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Tue, 08 Nov 2016 22:41:45 +0900 |
parents | ad56204f733e |
children | 39d13b8c101d |
comparison
equal
deleted
inserted
replaced
30374:ad56204f733e | 30375:5564fcd031df |
---|---|
207 | 207 |
208 try: | 208 try: |
209 for hname, cmd in hooks: | 209 for hname, cmd in hooks: |
210 if oldstdout == -1 and _redirect: | 210 if oldstdout == -1 and _redirect: |
211 try: | 211 try: |
212 stdoutno = sys.__stdout__.fileno() | 212 stdoutno = sys.stdout.fileno() |
213 stderrno = sys.__stderr__.fileno() | 213 stderrno = sys.stderr.fileno() |
214 # temporarily redirect stdout to stderr, if possible | 214 # temporarily redirect stdout to stderr, if possible |
215 if stdoutno >= 0 and stderrno >= 0: | 215 if stdoutno >= 0 and stderrno >= 0: |
216 sys.__stdout__.flush() | 216 sys.stdout.flush() |
217 oldstdout = os.dup(stdoutno) | 217 oldstdout = os.dup(stdoutno) |
218 os.dup2(stderrno, stdoutno) | 218 os.dup2(stderrno, stdoutno) |
219 except (OSError, AttributeError): | 219 except (OSError, AttributeError): |
220 # files seem to be bogus, give up on redirecting (WSGI, etc) | 220 # files seem to be bogus, give up on redirecting (WSGI, etc) |
221 pass | 221 pass |
256 # A forcible flush is required to make small stderr data in the | 256 # A forcible flush is required to make small stderr data in the |
257 # remote side available to the client immediately. | 257 # remote side available to the client immediately. |
258 sys.stderr.flush() | 258 sys.stderr.flush() |
259 finally: | 259 finally: |
260 if _redirect and oldstdout >= 0: | 260 if _redirect and oldstdout >= 0: |
261 sys.__stdout__.flush() # write hook output to stderr fd | 261 sys.stdout.flush() # write hook output to stderr fd |
262 os.dup2(oldstdout, stdoutno) | 262 os.dup2(oldstdout, stdoutno) |
263 os.close(oldstdout) | 263 os.close(oldstdout) |
264 | 264 |
265 return res | 265 return res |