##// END OF EJS Templates
procutil: redirect ui.fout to stderr while stdio is protected...
Yuya Nishihara -
r37237:7f78de1c default
parent child Browse files
Show More
@@ -213,25 +213,27 b' def isstdout(f):'
213 return _testfileno(f, sys.__stdout__)
213 return _testfileno(f, sys.__stdout__)
214
214
215 def protectstdio(uin, uout):
215 def protectstdio(uin, uout):
216 """Duplicate streams and redirect original to null if (uin, uout) are
216 """Duplicate streams and redirect original if (uin, uout) are stdio
217 stdio
217
218 If uin is stdin, it's redirected to /dev/null. If uout is stdout, it's
219 redirected to stderr so the output is still readable.
218
220
219 Returns (fin, fout) which point to the original (uin, uout) fds, but
221 Returns (fin, fout) which point to the original (uin, uout) fds, but
220 may be copy of (uin, uout). The returned streams can be considered
222 may be copy of (uin, uout). The returned streams can be considered
221 "owned" in that print(), exec(), etc. never reach to them.
223 "owned" in that print(), exec(), etc. never reach to them.
222 """
224 """
223 uout.flush()
225 uout.flush()
224 nullfd = os.open(os.devnull, os.O_RDWR)
225 fin, fout = uin, uout
226 fin, fout = uin, uout
226 if uin is stdin:
227 if uin is stdin:
227 newfd = os.dup(uin.fileno())
228 newfd = os.dup(uin.fileno())
229 nullfd = os.open(os.devnull, os.O_RDONLY)
228 os.dup2(nullfd, uin.fileno())
230 os.dup2(nullfd, uin.fileno())
231 os.close(nullfd)
229 fin = os.fdopen(newfd, r'rb')
232 fin = os.fdopen(newfd, r'rb')
230 if uout is stdout:
233 if uout is stdout:
231 newfd = os.dup(uout.fileno())
234 newfd = os.dup(uout.fileno())
232 os.dup2(nullfd, uout.fileno())
235 os.dup2(stderr.fileno(), uout.fileno())
233 fout = os.fdopen(newfd, r'wb')
236 fout = os.fdopen(newfd, r'wb')
234 os.close(nullfd)
235 return fin, fout
237 return fin, fout
236
238
237 def restorestdio(uin, uout, fin, fout):
239 def restorestdio(uin, uout, fin, fout):
@@ -249,6 +249,8 b' check that local configs for the cached '
249 ... input=stringio('some input'))
249 ... input=stringio('some input'))
250 *** runcommand --config hooks.pre-identify=python:hook.hook id
250 *** runcommand --config hooks.pre-identify=python:hook.hook id
251 eff892de26ec tip
251 eff892de26ec tip
252 hook talking
253 now try to read something: ''
252
254
253 Clean hook cached version
255 Clean hook cached version
254 $ rm hook.py*
256 $ rm hook.py*
@@ -619,7 +621,7 b' changelog and manifest would have invali'
619 > @command(b"debugwritestdout", norepo=True)
621 > @command(b"debugwritestdout", norepo=True)
620 > def debugwritestdout(ui):
622 > def debugwritestdout(ui):
621 > os.write(1, "low-level stdout fd and\n")
623 > os.write(1, "low-level stdout fd and\n")
622 > sys.stdout.write("stdout should be redirected to /dev/null\n")
624 > sys.stdout.write("stdout should be redirected to stderr\n")
623 > sys.stdout.flush()
625 > sys.stdout.flush()
624 > EOF
626 > EOF
625 $ cat <<EOF >> .hg/hgrc
627 $ cat <<EOF >> .hg/hgrc
@@ -657,6 +659,8 b' changelog and manifest would have invali'
657 *** runcommand debugreadstdin
659 *** runcommand debugreadstdin
658 read: ''
660 read: ''
659 *** runcommand debugwritestdout
661 *** runcommand debugwritestdout
662 low-level stdout fd and
663 stdout should be redirected to stderr
660
664
661
665
662 run commandserver in commandserver, which is silly but should work:
666 run commandserver in commandserver, which is silly but should work:
General Comments 0
You need to be logged in to leave comments. Login now