Patchwork [1,of,2] py3: make encoding.strio() an identity function on Python 2

login
register
mail settings
Submitter Yuya Nishihara
Date Aug. 16, 2017, 5:22 a.m.
Message ID <63c4583c7fa0f989f225.1502860958@mimosa>
Download mbox | patch
Permalink /patch/23069/
State Accepted
Headers show

Comments

Yuya Nishihara - Aug. 16, 2017, 5:22 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1502859011 -32400
#      Wed Aug 16 13:50:11 2017 +0900
# Node ID 63c4583c7fa0f989f225488273614f3a0aa88002
# Parent  627cb36b537f7f0ffe2ea7e38a9415b6c4c499e3
py3: make encoding.strio() an identity function on Python 2

It's the convention the other encoding.str*() functions follow. To make things
simple, this also drops kwargs from the strio() constructor.

Patch

diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -575,15 +575,17 @@  def fromutf8b(s):
         r += c
     return r
 
-class strio(io.TextIOWrapper):
-    """Wrapper around TextIOWrapper that respects hg's encoding assumptions.
+if pycompat.ispy3:
+    class strio(io.TextIOWrapper):
+        """Wrapper around TextIOWrapper that respects hg's encoding assumptions.
 
-    Also works around Python closing streams.
-    """
+        Also works around Python closing streams.
+        """
 
-    def __init__(self, buffer, **kwargs):
-        kwargs[r'encoding'] = _sysstr(encoding)
-        super(strio, self).__init__(buffer, **kwargs)
+        def __init__(self, buffer):
+            super(strio, self).__init__(buffer, encoding=_sysstr(encoding))
 
-    def __del__(self):
-        """Override __del__ so it doesn't close the underlying stream."""
+        def __del__(self):
+            """Override __del__ so it doesn't close the underlying stream."""
+else:
+    strio = pycompat.identity
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -175,11 +175,10 @@  def safehasattr(thing, attr):
 def bytesinput(fin, fout, *args, **kwargs):
     sin, sout = sys.stdin, sys.stdout
     try:
+        sys.stdin, sys.stdout = encoding.strio(fin), encoding.strio(fout)
         if pycompat.ispy3:
-            sys.stdin, sys.stdout = encoding.strio(fin), encoding.strio(fout)
             return encoding.strtolocal(input(*args, **kwargs))
         else:
-            sys.stdin, sys.stdout = fin, fout
             return raw_input(*args, **kwargs)
     finally:
         sys.stdin, sys.stdout = sin, sout