[PATCH 5 of 7] encoding: add converter between native str and byte string
Yuya Nishihara
yuya at tcha.org
Mon Mar 13 14:59:06 EDT 2017
# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1489421576 25200
# Mon Mar 13 09:12:56 2017 -0700
# Node ID 5a7ae0b9a19659dec4c951a86349728867265cab
# Parent fbc1a71bd3ac4a7e45c38c18680d242e935a1d2b
encoding: add converter between native str and byte string
This kind of encoding conversion is unavoidable on Python 3.
diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -204,6 +204,16 @@ def unifromlocal(s):
"""Convert a byte string of local encoding to a unicode string"""
return fromlocal(s).decode('utf-8')
+# converter functions between native str and byte string. use these if the
+# character encoding is not aware (e.g. exception message) or is known to
+# be locale dependent (e.g. date formatting.)
+if pycompat.ispy3:
+ strtolocal = unitolocal
+ strfromlocal = unifromlocal
+else:
+ strtolocal = str
+ strfromlocal = str
+
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
@@ -108,9 +108,7 @@ def _importext(name, path=None, reportfu
def _forbytes(inst):
"""Portably format an import error into a form suitable for
%-formatting into bytestrings."""
- if pycompat.ispy3:
- return encoding.unitolocal(str(inst))
- return inst
+ return encoding.strtolocal(str(inst))
def _reportimporterror(ui, err, failed, next):
# note: this ui.debug happens before --debug is processed,
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -2651,9 +2651,7 @@ class url(object):
>>> print url(r'file:///D:\data\hg')
file:///D:\data\hg
"""
- if pycompat.ispy3:
- return encoding.unifromlocal(self.__bytes__())
- return self.__bytes__()
+ return encoding.strfromlocal(self.__bytes__())
def __bytes__(self):
if self._localpath:
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:211:
+ mercurial/encoding.py:221:
> 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