[PATCH 4 of 7] encoding: factor out unicode variants of from/tolocal()
Yuya Nishihara
yuya at tcha.org
Mon Mar 13 14:59:05 EDT 2017
# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1489421468 25200
# Mon Mar 13 09:11:08 2017 -0700
# Node ID fbc1a71bd3ac4a7e45c38c18680d242e935a1d2b
# Parent 802478258f49c11e5248baa674bdfd7145882d93
encoding: factor out unicode variants of from/tolocal()
Unfortunately, these functions will be commonly used on Python 3.
diff --git a/hgext/convert/subversion.py b/hgext/convert/subversion.py
--- a/hgext/convert/subversion.py
+++ b/hgext/convert/subversion.py
@@ -1186,7 +1186,7 @@ class svn_sink(converter_sink, commandli
# 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
diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -196,6 +196,14 @@ def fromlocal(s):
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
diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -109,7 +109,7 @@ def _forbytes(inst):
"""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):
diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -353,4 +353,4 @@ def headdecode(s):
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))
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -2652,7 +2652,7 @@ class url(object):
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):
diff --git a/mercurial/windows.py b/mercurial/windows.py
--- a/mercurial/windows.py
+++ b/mercurial/windows.py
@@ -442,7 +442,7 @@ def lookupreg(key, valname=None, scope=N
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
diff --git a/tests/test-check-code.t b/tests/test-check-code.t
--- a/tests/test-check-code.t
+++ b/tests/test-check-code.t
@@ -22,7 +22,7 @@ New errors are not allowed. Warnings are
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)
More information about the Mercurial-devel
mailing list