Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/utils/procutil.py @ 45043:fd205a9c358a
procutil: factor out conditional creation of LineBufferedWrapper
At the same time, document the logic and generalize it to work on all Python
versions.
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Sat, 04 Jul 2020 12:15:41 +0200 |
parents | 2bfbd7d2c204 |
children | 1053f878dd46 |
comparison
equal
deleted
inserted
replaced
45042:2bfbd7d2c204 | 45043:fd205a9c358a |
---|---|
63 orig.flush() | 63 orig.flush() |
64 return res | 64 return res |
65 | 65 |
66 | 66 |
67 io.BufferedIOBase.register(LineBufferedWrapper) | 67 io.BufferedIOBase.register(LineBufferedWrapper) |
68 | |
69 | |
70 def make_line_buffered(stream): | |
71 if pycompat.ispy3 and not isinstance(stream, io.BufferedIOBase): | |
72 # On Python 3, buffered streams can be expected to subclass | |
73 # BufferedIOBase. This is definitively the case for the streams | |
74 # initialized by the interpreter. For unbuffered streams, we don't need | |
75 # to emulate line buffering. | |
76 return stream | |
77 if isinstance(stream, LineBufferedWrapper): | |
78 return stream | |
79 return LineBufferedWrapper(stream) | |
68 | 80 |
69 | 81 |
70 # glibc determines buffering on first write to stdout - if we replace a TTY | 82 # glibc determines buffering on first write to stdout - if we replace a TTY |
71 # destined stdout with a pipe destined stdout (e.g. pager), we want line | 83 # destined stdout with a pipe destined stdout (e.g. pager), we want line |
72 # buffering (or unbuffered, on Windows) | 84 # buffering (or unbuffered, on Windows) |
75 # Windows doesn't support line buffering | 87 # Windows doesn't support line buffering |
76 stdout = os.fdopen(stdout.fileno(), 'wb', 0) | 88 stdout = os.fdopen(stdout.fileno(), 'wb', 0) |
77 elif pycompat.ispy3: | 89 elif pycompat.ispy3: |
78 # On Python 3, buffered binary streams can't be set line-buffered. | 90 # On Python 3, buffered binary streams can't be set line-buffered. |
79 # Therefore we have a wrapper that implements line buffering. | 91 # Therefore we have a wrapper that implements line buffering. |
80 if isinstance(stdout, io.BufferedIOBase) and not isinstance( | 92 stdout = make_line_buffered(stdout) |
81 stdout, LineBufferedWrapper | |
82 ): | |
83 stdout = LineBufferedWrapper(stdout) | |
84 else: | 93 else: |
85 stdout = os.fdopen(stdout.fileno(), 'wb', 1) | 94 stdout = os.fdopen(stdout.fileno(), 'wb', 1) |
86 | 95 |
87 if pycompat.iswindows: | 96 if pycompat.iswindows: |
88 from .. import windows as platform | 97 from .. import windows as platform |