Patchwork [07,of,11,V2] procutil: distribute code for stdout

login
register
mail settings
Submitter Manuel Jacob
Date July 12, 2020, 10:41 p.m.
Message ID <2151604d856a8cd9c204.1594593692@tmp>
Download mbox | patch
Permalink /patch/46711/
State Accepted
Headers show

Comments

Manuel Jacob - July 12, 2020, 10:41 p.m.
# HG changeset patch
# User Manuel Jacob <me@manueljacob.de>
# Date 1594368724 -7200
#      Fri Jul 10 10:12:04 2020 +0200
# Node ID 2151604d856a8cd9c2049e785a995714f49be115
# Parent  472cbd166fe075b8786d2965a44b4e5d1c26bdf5
# EXP-Topic stdio
procutil: distribute code for stdout

It makes sense to have the distinction between Python 2 and 3 at the top level,
as we have to fight a different kind of battle on each: On Python 3, we get
consistent behavior on all platforms, but need to create correctly-behaving
binary streams. On Python 2, we have to account for platform differences.
Manuel Jacob - July 12, 2020, 11:25 p.m.
This is not as much of an improvement as it was before the code for 
stderr was removed. But I think it’s still a little improvement and a 
later patch depends on it, so I decided to send it anyway.

On 2020-07-13 00:41, Manuel Jacob wrote:
> # HG changeset patch
> # User Manuel Jacob <me@manueljacob.de>
> # Date 1594368724 -7200
> #      Fri Jul 10 10:12:04 2020 +0200
> # Node ID 2151604d856a8cd9c2049e785a995714f49be115
> # Parent  472cbd166fe075b8786d2965a44b4e5d1c26bdf5
> # EXP-Topic stdio
> procutil: distribute code for stdout
> 
> It makes sense to have the distinction between Python 2 and 3 at the 
> top level,
> as we have to fight a different kind of battle on each: On Python 3, we 
> get
> consistent behavior on all platforms, but need to create 
> correctly-behaving
> binary streams. On Python 2, we have to account for platform 
> differences.
> 
> diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py
> --- a/mercurial/utils/procutil.py
> +++ b/mercurial/utils/procutil.py
> @@ -102,32 +102,31 @@
> 
> 
>  if pycompat.ispy3:
> +    # Python 3 implements its own I/O streams.
>      # TODO: .buffer might not exist if std streams were replaced; 
> we'll need
>      # a silly wrapper to make a bytes stream backed by a unicode one.
>      stdin = sys.stdin.buffer
>      stdout = sys.stdout.buffer
>      stderr = sys.stderr.buffer
> +    if isatty(stdout):
> +        # The standard library doesn't offer line-buffered binary 
> streams.
> +        stdout = make_line_buffered(stdout)
>  else:
> +    # Python 2 uses the I/O streams provided by the C library.
>      stdin = sys.stdin
>      stdout = sys.stdout
>      stderr = sys.stderr
> -
> -if isatty(stdout):
> -    if pycompat.ispy3:
> -        # Python 3 implements its own I/O streams.
> -        # The standard library doesn't offer line-buffered binary 
> streams.
> -        stdout = make_line_buffered(stdout)
> -    elif pycompat.iswindows:
> -        # Work around size limit when writing to console.
> -        stdout = platform.winstdout(stdout)
> -        # Python 2 uses the I/O streams provided by the C library.
> -        # The Windows C runtime library doesn't support line 
> buffering.
> -        stdout = make_line_buffered(stdout)
> -    else:
> -        # glibc determines buffering on first write to stdout - if we
> -        # replace a TTY destined stdout with a pipe destined stdout 
> (e.g.
> -        # pager), we want line buffering.
> -        stdout = reopen_stream(stdout, 'wb', 1)
> +    if isatty(stdout):
> +        if pycompat.iswindows:
> +            # Work around size limit when writing to console.
> +            stdout = platform.winstdout(stdout)
> +            # The Windows C runtime library doesn't support line 
> buffering.
> +            stdout = make_line_buffered(stdout)
> +        else:
> +            # glibc determines buffering on first write to stdout - if 
> we
> +            # replace a TTY destined stdout with a pipe destined 
> stdout (e.g.
> +            # pager), we want line buffering.
> +            stdout = reopen_stream(stdout, 'wb', 1)
> 
> 
>  findexe = platform.findexe
> 
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py
--- a/mercurial/utils/procutil.py
+++ b/mercurial/utils/procutil.py
@@ -102,32 +102,31 @@ 
 
 
 if pycompat.ispy3:
+    # Python 3 implements its own I/O streams.
     # TODO: .buffer might not exist if std streams were replaced; we'll need
     # a silly wrapper to make a bytes stream backed by a unicode one.
     stdin = sys.stdin.buffer
     stdout = sys.stdout.buffer
     stderr = sys.stderr.buffer
+    if isatty(stdout):
+        # The standard library doesn't offer line-buffered binary streams.
+        stdout = make_line_buffered(stdout)
 else:
+    # Python 2 uses the I/O streams provided by the C library.
     stdin = sys.stdin
     stdout = sys.stdout
     stderr = sys.stderr
-
-if isatty(stdout):
-    if pycompat.ispy3:
-        # Python 3 implements its own I/O streams.
-        # The standard library doesn't offer line-buffered binary streams.
-        stdout = make_line_buffered(stdout)
-    elif pycompat.iswindows:
-        # Work around size limit when writing to console.
-        stdout = platform.winstdout(stdout)
-        # Python 2 uses the I/O streams provided by the C library.
-        # The Windows C runtime library doesn't support line buffering.
-        stdout = make_line_buffered(stdout)
-    else:
-        # glibc determines buffering on first write to stdout - if we
-        # replace a TTY destined stdout with a pipe destined stdout (e.g.
-        # pager), we want line buffering.
-        stdout = reopen_stream(stdout, 'wb', 1)
+    if isatty(stdout):
+        if pycompat.iswindows:
+            # Work around size limit when writing to console.
+            stdout = platform.winstdout(stdout)
+            # The Windows C runtime library doesn't support line buffering.
+            stdout = make_line_buffered(stdout)
+        else:
+            # glibc determines buffering on first write to stdout - if we
+            # replace a TTY destined stdout with a pipe destined stdout (e.g.
+            # pager), we want line buffering.
+            stdout = reopen_stream(stdout, 'wb', 1)
 
 
 findexe = platform.findexe