mercurial/utils/procutil.py
branchstable
changeset 49378 094a5fa3cf52
parent 49084 ea98850a136e
child 49793 8147abc05794
--- a/mercurial/utils/procutil.py	Tue Jul 05 17:53:26 2022 +0200
+++ b/mercurial/utils/procutil.py	Mon Jul 11 01:51:20 2022 +0200
@@ -80,12 +80,32 @@
 
 
 def make_line_buffered(stream):
-    if 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.
+    # First, check if we need to wrap the stream.
+    check_stream = stream
+    while True:
+        if isinstance(check_stream, WriteAllWrapper):
+            check_stream = check_stream.orig
+        elif pycompat.iswindows and isinstance(
+            check_stream,
+            # pytype: disable=module-attr
+            platform.winstdout
+            # pytype: enable=module-attr
+        ):
+            check_stream = check_stream.fp
+        else:
+            break
+    if isinstance(check_stream, io.RawIOBase):
+        # The stream is unbuffered, we don't need to emulate line buffering.
         return stream
+    elif isinstance(check_stream, io.BufferedIOBase):
+        # The stream supports some kind of buffering. We can't assume that
+        # lines are flushed. Fall back to wrapping the stream.
+        pass
+    else:
+        raise NotImplementedError(
+            "can't determine whether stream is buffered or not"
+        )
+
     if isinstance(stream, LineBufferedWrapper):
         return stream
     return LineBufferedWrapper(stream)