[PATCH 3 of 5 RESEND] py3: provide bytes stdin/out/err through util module

Yuya Nishihara yuya at tcha.org
Fri Nov 11 10:50:22 EST 2016


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1476974424 -32400
#      Thu Oct 20 23:40:24 2016 +0900
# Node ID 881654b85952ee9d7fa8e511ef1de63166f31211
# Parent  b5992be53acb8080b0f92c2756d7eb8d0e221d0c
py3: provide bytes stdin/out/err through util module

Since standard streams are TextIO on Python 3, we can't use sys.stdin/out/err
directly. Fortunately we can get the underlying BytesIO via .buffer as long as
the streams aren't replaced by e.g. StringIO.

stdin/out/err are provided through util so we can wrap them by platform API.

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -44,6 +44,12 @@ if ispy3:
     ospathsep = os.pathsep.encode('ascii')
     ossep = os.sep.encode('ascii')
 
+    # 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
+
     # Since Python 3 converts argv to wchar_t type by Py_DecodeLocale() on Unix,
     # we can use os.fsencode() to get back bytes argv.
     #
@@ -100,6 +106,9 @@ else:
     osname = os.name
     ospathsep = os.pathsep
     ossep = os.sep
+    stdin = sys.stdin
+    stdout = sys.stdout
+    stderr = sys.stderr
     sysargv = sys.argv
 
 stringio = io.StringIO
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -52,6 +52,9 @@ httpserver = pycompat.httpserver
 pickle = pycompat.pickle
 queue = pycompat.queue
 socketserver = pycompat.socketserver
+stderr = pycompat.stderr
+stdin = pycompat.stdin
+stdout = pycompat.stdout
 stringio = pycompat.stringio
 urlerr = pycompat.urlerr
 urlparse = pycompat.urlparse
@@ -60,6 +63,7 @@ xmlrpclib = pycompat.xmlrpclib
 
 if os.name == 'nt':
     from . import windows as platform
+    stdout = platform.winstdout(pycompat.stdout)
 else:
     from . import posix as platform
 


More information about the Mercurial-devel mailing list