Patchwork [2,of,2] pycompat: remove pycompat.{stdin,stdout,stderr}

login
register
mail settings
Submitter Manuel Jacob
Date July 8, 2020, 4:07 a.m.
Message ID <2c8a747c4c45c7bbbfba.1594181220@tmp>
Download mbox | patch
Permalink /patch/46653/
State Accepted
Headers show

Comments

Manuel Jacob - July 8, 2020, 4:07 a.m.
# HG changeset patch
# User Manuel Jacob <me@manueljacob.de>
# Date 1594050678 -7200
#      Mon Jul 06 17:51:18 2020 +0200
# Node ID 2c8a747c4c45c7bbbfbace45a601c8d90edc0c6c
# Parent  e06f86fc9e488722f691d4f9ac1d2d2e7d38bdd2
# EXP-Topic stdio
pycompat: remove pycompat.{stdin,stdout,stderr}

All users have been changed to use procutil.{stdin,stdout,stderr}, which provide
consistent behavior across platforms and Python versions.

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -142,17 +142,6 @@ 
 
     long = int
 
-    # Warning: sys.stdout.buffer and sys.stderr.buffer do not necessarily have
-    # the same buffering behavior as sys.stdout and sys.stderr. The interpreter
-    # initializes them with block-buffered streams or unbuffered streams (when
-    # the -u option or the PYTHONUNBUFFERED environment variable is set), never
-    # with a line-buffered stream.
-    # 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 getattr(sys, 'argv', None) is not None:
         # On POSIX, the char** argv array is converted to Python str using
         # Py_DecodeLocale(). The inverse of this is Py_EncodeLocale(), which
@@ -476,9 +465,6 @@ 
     osaltsep = os.altsep
     osdevnull = os.devnull
     long = long
-    stdin = sys.stdin
-    stdout = sys.stdout
-    stderr = sys.stderr
     if getattr(sys, 'argv', None) is not None:
         sysargv = sys.argv
     sysplatform = sys.platform
diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py
--- a/mercurial/utils/procutil.py
+++ b/mercurial/utils/procutil.py
@@ -80,9 +80,16 @@ 
     return LineBufferedWrapper(stream)
 
 
-stderr = pycompat.stderr
-stdin = pycompat.stdin
-stdout = pycompat.stdout
+if pycompat.ispy3:
+    # 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
+else:
+    stdin = sys.stdin
+    stdout = sys.stdout
+    stderr = sys.stderr
 
 if pycompat.iswindows:
     stdout = platform.winstdout(stdout)