changeset 30472:277f4fe6d01a

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.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 20 Oct 2016 23:40:24 +0900
parents 00c9ac4ce816
children 39d13b8c101d
files mercurial/pycompat.py mercurial/util.py
diffstat 2 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/pycompat.py	Fri Oct 21 00:09:38 2016 +0900
+++ b/mercurial/pycompat.py	Thu Oct 20 23:40:24 2016 +0900
@@ -44,6 +44,12 @@
     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 @@
     osname = os.name
     ospathsep = os.pathsep
     ossep = os.sep
+    stdin = sys.stdin
+    stdout = sys.stdout
+    stderr = sys.stderr
     sysargv = sys.argv
 
 stringio = io.StringIO
--- a/mercurial/util.py	Fri Oct 21 00:09:38 2016 +0900
+++ b/mercurial/util.py	Thu Oct 20 23:40:24 2016 +0900
@@ -54,6 +54,9 @@
 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
@@ -62,6 +65,7 @@
 
 if os.name == 'nt':
     from . import windows as platform
+    stdout = platform.winstdout(pycompat.stdout)
 else:
     from . import posix as platform