Mercurial > public > mercurial-scm > hg
comparison mercurial/utils/procutil.py @ 37124:6715e8035b4f
procutil: introduce context-manager interface for protect/restorestdio
The code looks slightly cleaner since it was pretty easy to pass arguments
in wrong order.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 25 Mar 2018 11:58:05 +0900 |
parents | 0216232f21ab |
children | ac71cbad5da3 |
comparison
equal
deleted
inserted
replaced
37123:0216232f21ab | 37124:6715e8035b4f |
---|---|
7 # This software may be used and distributed according to the terms of the | 7 # This software may be used and distributed according to the terms of the |
8 # GNU General Public License version 2 or any later version. | 8 # GNU General Public License version 2 or any later version. |
9 | 9 |
10 from __future__ import absolute_import | 10 from __future__ import absolute_import |
11 | 11 |
12 import contextlib | |
12 import imp | 13 import imp |
13 import io | 14 import io |
14 import os | 15 import os |
15 import signal | 16 import signal |
16 import subprocess | 17 import subprocess |
238 for f, uif in [(fin, uin), (fout, uout)]: | 239 for f, uif in [(fin, uin), (fout, uout)]: |
239 if f is not uif: | 240 if f is not uif: |
240 os.dup2(f.fileno(), uif.fileno()) | 241 os.dup2(f.fileno(), uif.fileno()) |
241 f.close() | 242 f.close() |
242 | 243 |
244 @contextlib.contextmanager | |
245 def protectedstdio(uin, uout): | |
246 """Run code block with protected standard streams""" | |
247 fin, fout = protectstdio(uin, uout) | |
248 try: | |
249 yield fin, fout | |
250 finally: | |
251 restorestdio(uin, uout, fin, fout) | |
252 | |
243 def shellenviron(environ=None): | 253 def shellenviron(environ=None): |
244 """return environ with optional override, useful for shelling out""" | 254 """return environ with optional override, useful for shelling out""" |
245 def py2shell(val): | 255 def py2shell(val): |
246 'convert python object into string that is useful to shell' | 256 'convert python object into string that is useful to shell' |
247 if val is None or val is False: | 257 if val is None or val is False: |