##// END OF EJS Templates
py3: make stdout line-buffered if connected to a TTY...
Manuel Jacob -
r45477:f9734b2d default
parent child Browse files
Show More
@@ -49,6 +49,25 b' def isatty(fp):'
49 return False
49 return False
50
50
51
51
52 if pycompat.ispy3:
53
54 class LineBufferedWrapper(object):
55 def __init__(self, orig):
56 self.orig = orig
57
58 def __getattr__(self, attr):
59 return getattr(self.orig, attr)
60
61 def write(self, s):
62 orig = self.orig
63 res = orig.write(s)
64 if s.endswith(b'\n'):
65 orig.flush()
66 return res
67
68 io.BufferedIOBase.register(LineBufferedWrapper)
69
70
52 # glibc determines buffering on first write to stdout - if we replace a TTY
71 # glibc determines buffering on first write to stdout - if we replace a TTY
53 # destined stdout with a pipe destined stdout (e.g. pager), we want line
72 # destined stdout with a pipe destined stdout (e.g. pager), we want line
54 # buffering (or unbuffered, on Windows)
73 # buffering (or unbuffered, on Windows)
@@ -56,9 +75,14 b' if isatty(stdout):'
56 if pycompat.iswindows:
75 if pycompat.iswindows:
57 # Windows doesn't support line buffering
76 # Windows doesn't support line buffering
58 stdout = os.fdopen(stdout.fileno(), 'wb', 0)
77 stdout = os.fdopen(stdout.fileno(), 'wb', 0)
59 elif not pycompat.ispy3:
78 elif pycompat.ispy3:
60 # on Python 3, stdout (sys.stdout.buffer) is already line buffered and
79 # On Python 3, buffered binary streams can't be set line-buffered.
61 # buffering=1 is not handled in binary mode
80 # Therefore we have a wrapper that implements line buffering.
81 if isinstance(stdout, io.BufferedIOBase) and not isinstance(
82 stdout, LineBufferedWrapper
83 ):
84 stdout = LineBufferedWrapper(stdout)
85 else:
62 stdout = os.fdopen(stdout.fileno(), 'wb', 1)
86 stdout = os.fdopen(stdout.fileno(), 'wb', 1)
63
87
64 if pycompat.iswindows:
88 if pycompat.iswindows:
General Comments 0
You need to be logged in to leave comments. Login now