changeset 49377:094a5fa3cf52 stable 6.2

procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.
author Manuel Jacob <me@manueljacob.de>
date Mon, 11 Jul 2022 01:51:20 +0200
parents 7fe86498e84f
children b0ab20f24171
files mercurial/utils/procutil.py
diffstat 1 files changed, 25 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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)