Patchwork [stable,v2] py3: always flush ui streams on Python 3

login
register
mail settings
Submitter Manuel Jacob
Date June 3, 2020, 4:11 a.m.
Message ID <6d7faa3c9b9cebd46ff7.1591157483@tmp>
Download mbox | patch
Permalink /patch/46459/
State New
Headers show

Comments

Manuel Jacob - June 3, 2020, 4:11 a.m.
# HG changeset patch
# User Manuel Jacob <me@manueljacob.de>
# Date 1591120869 -7200
#      Tue Jun 02 20:01:09 2020 +0200
# Branch stable
# Node ID 6d7faa3c9b9cebd46ff7964be703d5b67d6eda50
# Parent  91e509a12dbc4cd6cf2dcb9dae3ed383932132ac
py3: always flush ui streams on Python 3

On Python 2, we rely on that stdout and stderr are line buffered. Python 3’s
io module doesn’t offer line buffered binary streams.

To trigger the problem with the hg command, you need to do something slow,
e.g. `hg pull https://www.mercurial-scm.org/repo/hg`.

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1198,9 +1198,14 @@  class ui(object):
                     label = opts.get('label', b'')
                     msg = self.label(msg, label)
                 dest.write(msg)
+            # On Python 2, stdout and stderr are usually line buffered, but
             # stderr may be buffered under win32 when redirected to files,
             # including stdout.
-            if dest is self._ferr and not getattr(self._ferr, 'closed', False):
+            # On Python 3, we use the underlying binary buffer, which does not
+            # support line buffering.
+            if (pycompat.ispy3 or dest is self._ferr) and not getattr(
+                dest, 'closed', False
+            ):
                 dest.flush()
         except IOError as err:
             if dest is self._ferr and err.errno in (