78 io.BufferedIOBase.register(LineBufferedWrapper) |
78 io.BufferedIOBase.register(LineBufferedWrapper) |
79 # pytype: enable=attribute-error |
79 # pytype: enable=attribute-error |
80 |
80 |
81 |
81 |
82 def make_line_buffered(stream): |
82 def make_line_buffered(stream): |
83 if not isinstance(stream, io.BufferedIOBase): |
83 # First, check if we need to wrap the stream. |
84 # On Python 3, buffered streams can be expected to subclass |
84 check_stream = stream |
85 # BufferedIOBase. This is definitively the case for the streams |
85 while True: |
86 # initialized by the interpreter. For unbuffered streams, we don't need |
86 if isinstance(check_stream, WriteAllWrapper): |
87 # to emulate line buffering. |
87 check_stream = check_stream.orig |
|
88 elif pycompat.iswindows and isinstance( |
|
89 check_stream, |
|
90 # pytype: disable=module-attr |
|
91 platform.winstdout |
|
92 # pytype: enable=module-attr |
|
93 ): |
|
94 check_stream = check_stream.fp |
|
95 else: |
|
96 break |
|
97 if isinstance(check_stream, io.RawIOBase): |
|
98 # The stream is unbuffered, we don't need to emulate line buffering. |
88 return stream |
99 return stream |
|
100 elif isinstance(check_stream, io.BufferedIOBase): |
|
101 # The stream supports some kind of buffering. We can't assume that |
|
102 # lines are flushed. Fall back to wrapping the stream. |
|
103 pass |
|
104 else: |
|
105 raise NotImplementedError( |
|
106 "can't determine whether stream is buffered or not" |
|
107 ) |
|
108 |
89 if isinstance(stream, LineBufferedWrapper): |
109 if isinstance(stream, LineBufferedWrapper): |
90 return stream |
110 return stream |
91 return LineBufferedWrapper(stream) |
111 return LineBufferedWrapper(stream) |
92 |
112 |
93 |
113 |