##// END OF EJS Templates
posix: implement readpipe using non-blocking I/O (issue4336)...
Gregory Szorc -
r22246:331cbf08 default
parent child Browse files
Show More
@@ -8,6 +8,7
8 from i18n import _
8 from i18n import _
9 import encoding
9 import encoding
10 import os, sys, errno, stat, getpass, pwd, grp, socket, tempfile, unicodedata
10 import os, sys, errno, stat, getpass, pwd, grp, socket, tempfile, unicodedata
11 import fcntl
11
12
12 posixfile = open
13 posixfile = open
13 normpath = os.path.normpath
14 normpath = os.path.normpath
@@ -432,7 +433,7 def gethgcmd():
432
433
433 def termwidth():
434 def termwidth():
434 try:
435 try:
435 import termios, array, fcntl
436 import termios, array
436 for dev in (sys.stderr, sys.stdout, sys.stdin):
437 for dev in (sys.stderr, sys.stdout, sys.stdin):
437 try:
438 try:
438 try:
439 try:
@@ -570,13 +571,24 def statisexec(st):
570
571
571 def readpipe(pipe):
572 def readpipe(pipe):
572 """Read all available data from a pipe."""
573 """Read all available data from a pipe."""
574 # We can't fstat() a pipe because Linux will always report 0.
575 # So, we set the pipe to non-blocking mode and read everything
576 # that's available.
577 flags = fcntl.fcntl(pipe, fcntl.F_GETFL)
578 flags |= os.O_NONBLOCK
579 oldflags = fcntl.fcntl(pipe, fcntl.F_SETFL, flags)
580
581 try:
573 chunks = []
582 chunks = []
574 while True:
583 while True:
575 size = os.fstat(pipe.fileno()).st_size
584 try:
576 if not size:
585 s = pipe.read()
577 break
578
579 s = pipe.read(size)
580 if not s:
586 if not s:
581 break
587 break
582 chunks.append(s)
588 chunks.append(s)
589 except IOError:
590 break
591
592 return ''.join(chunks)
593 finally:
594 fcntl.fcntl(pipe, fcntl.F_SETFL, oldflags)
General Comments 0
You need to be logged in to leave comments. Login now