Read about our upcoming Code of Conduct on this issue

Commit d0a94412 authored by Manuel Jacob's avatar Manuel Jacob
Browse files

procutil: factor out conditional creation of LineBufferedWrapper

At the same time, document the logic and generalize it to work on all Python
versions.
parent 2bfbd7d2c204
......@@ -67,6 +67,18 @@ class LineBufferedWrapper(object):
io.BufferedIOBase.register(LineBufferedWrapper)
def make_line_buffered(stream):
if pycompat.ispy3 and not isinstance(stream, io.BufferedIOBase):
# On Python 3, buffered streams can be expected to subclass
# BufferedIOBase. This is definitively the case for the streams
# initialized by the interpreter. For unbuffered streams, we don't need
# to emulate line buffering.
return stream
if isinstance(stream, LineBufferedWrapper):
return stream
return LineBufferedWrapper(stream)
# glibc determines buffering on first write to stdout - if we replace a TTY
# destined stdout with a pipe destined stdout (e.g. pager), we want line
# buffering (or unbuffered, on Windows)
......@@ -77,10 +89,7 @@ if isatty(stdout):
elif pycompat.ispy3:
# On Python 3, buffered binary streams can't be set line-buffered.
# Therefore we have a wrapper that implements line buffering.
if isinstance(stdout, io.BufferedIOBase) and not isinstance(
stdout, LineBufferedWrapper
):
stdout = LineBufferedWrapper(stdout)
stdout = make_line_buffered(stdout)
else:
stdout = os.fdopen(stdout.fileno(), 'wb', 1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment