changeset 31447:067add650129

encoding: factor out unicode variants of from/tolocal() Unfortunately, these functions will be commonly used on Python 3.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 13 Mar 2017 09:11:08 -0700
parents 9a94239e4f12
children 6419cd243017
files hgext/convert/subversion.py mercurial/encoding.py mercurial/extensions.py mercurial/mail.py mercurial/util.py mercurial/windows.py tests/test-check-code.t
diffstat 7 files changed, 14 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/convert/subversion.py	Mon Mar 13 08:53:31 2017 -0700
+++ b/hgext/convert/subversion.py	Mon Mar 13 09:11:08 2017 -0700
@@ -1186,7 +1186,7 @@
                 # best bet is to assume they are in local
                 # encoding. They will be passed to command line calls
                 # later anyway, so they better be.
-                m.add(encoding.tolocal(name.encode('utf-8')))
+                m.add(encoding.unitolocal(name))
                 break
         return m
 
--- a/mercurial/encoding.py	Mon Mar 13 08:53:31 2017 -0700
+++ b/mercurial/encoding.py	Mon Mar 13 09:11:08 2017 -0700
@@ -196,6 +196,14 @@
     except LookupError as k:
         raise error.Abort(k, hint="please check your locale settings")
 
+def unitolocal(u):
+    """Convert a unicode string to a byte string of local encoding"""
+    return tolocal(u.encode('utf-8'))
+
+def unifromlocal(s):
+    """Convert a byte string of local encoding to a unicode string"""
+    return fromlocal(s).decode('utf-8')
+
 if not _nativeenviron:
     # now encoding and helper functions are available, recreate the environ
     # dict to be exported to other modules
--- a/mercurial/extensions.py	Mon Mar 13 08:53:31 2017 -0700
+++ b/mercurial/extensions.py	Mon Mar 13 09:11:08 2017 -0700
@@ -109,7 +109,7 @@
     """Portably format an import error into a form suitable for
     %-formatting into bytestrings."""
     if pycompat.ispy3:
-        return encoding.tolocal(str(inst).encode('utf-8'))
+        return encoding.unitolocal(str(inst))
     return inst
 
 def _reportimporterror(ui, err, failed, next):
--- a/mercurial/mail.py	Mon Mar 13 08:53:31 2017 -0700
+++ b/mercurial/mail.py	Mon Mar 13 09:11:08 2017 -0700
@@ -353,4 +353,4 @@
         except UnicodeDecodeError:
             pass
         uparts.append(part.decode('ISO-8859-1'))
-    return encoding.tolocal(u' '.join(uparts).encode('UTF-8'))
+    return encoding.unitolocal(u' '.join(uparts))
--- a/mercurial/util.py	Mon Mar 13 08:53:31 2017 -0700
+++ b/mercurial/util.py	Mon Mar 13 09:11:08 2017 -0700
@@ -2654,7 +2654,7 @@
         file:///D:\data\hg
         """
         if pycompat.ispy3:
-            return encoding.fromlocal(self.__bytes__()).decode('utf-8')
+            return encoding.unifromlocal(self.__bytes__())
         return self.__bytes__()
 
     def __bytes__(self):
--- a/mercurial/windows.py	Mon Mar 13 08:53:31 2017 -0700
+++ b/mercurial/windows.py	Mon Mar 13 09:11:08 2017 -0700
@@ -442,7 +442,7 @@
         try:
             val = winreg.QueryValueEx(winreg.OpenKey(s, key), valname)[0]
             # never let a Unicode string escape into the wild
-            return encoding.tolocal(val.encode('UTF-8'))
+            return encoding.unitolocal(val)
         except EnvironmentError:
             pass
 
--- a/tests/test-check-code.t	Mon Mar 13 08:53:31 2017 -0700
+++ b/tests/test-check-code.t	Mon Mar 13 09:11:08 2017 -0700
@@ -22,7 +22,7 @@
   mercurial/encoding.py:61:
    >                    for k, v in os.environ.items())
    use encoding.environ instead (py3)
-  mercurial/encoding.py:203:
+  mercurial/encoding.py:211:
    >                    for k, v in os.environ.items())
    use encoding.environ instead (py3)
   Skipping mercurial/httpclient/__init__.py it has no-che?k-code (glob)