encoding: use raw strings for encoding arguments
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 02 Mar 2019 13:07:58 -0800
changeset 41836 25694a78e4a4
parent 41835 ddb174511f1b
child 41837 82d9728ace95
encoding: use raw strings for encoding arguments This prevents the internals of Python from coercing a unicode to str on Python 2 and makes tests run with HGUNICODEPEDANTRY=1 a lot happier. Differential Revision: https://phab.mercurial-scm.org/D6051
mercurial/encoding.py
--- a/mercurial/encoding.py	Sat Mar 02 13:02:39 2019 -0800
+++ b/mercurial/encoding.py	Sat Mar 02 13:07:58 2019 -0800
@@ -65,7 +65,7 @@
 else:
     # preferred encoding isn't known yet; use utf-8 to avoid unicode error
     # and recreate it once encoding is settled
-    environ = dict((k.encode(u'utf-8'), v.encode(u'utf-8'))
+    environ = dict((k.encode(r'utf-8'), v.encode(r'utf-8'))
                    for k, v in os.environ.items())  # re-exports
 
 _encodingrewrites = {
@@ -152,7 +152,7 @@
             if encoding == 'UTF-8':
                 # fast path
                 return s
-            r = u.encode(_sysstr(encoding), u"replace")
+            r = u.encode(_sysstr(encoding), r"replace")
             if u == r.decode(_sysstr(encoding)):
                 # r is a safe, non-lossy encoding of s
                 return safelocalstr(r)
@@ -161,7 +161,7 @@
             # we should only get here if we're looking at an ancient changeset
             try:
                 u = s.decode(_sysstr(fallbackencoding))
-                r = u.encode(_sysstr(encoding), u"replace")
+                r = u.encode(_sysstr(encoding), r"replace")
                 if u == r.decode(_sysstr(encoding)):
                     # r is a safe, non-lossy encoding of s
                     return safelocalstr(r)
@@ -169,7 +169,7 @@
             except UnicodeDecodeError:
                 u = s.decode("utf-8", "replace") # last ditch
                 # can't round-trip
-                return u.encode(_sysstr(encoding), u"replace")
+                return u.encode(_sysstr(encoding), r"replace")
     except LookupError as k:
         raise error.Abort(k, hint="please check your locale settings")
 
@@ -230,7 +230,7 @@
 if not _nativeenviron:
     # now encoding and helper functions are available, recreate the environ
     # dict to be exported to other modules
-    environ = dict((tolocal(k.encode(u'utf-8')), tolocal(v.encode(u'utf-8')))
+    environ = dict((tolocal(k.encode(r'utf-8')), tolocal(v.encode(r'utf-8')))
                    for k, v in os.environ.items())  # re-exports
 
 if pycompat.ispy3:
@@ -251,7 +251,7 @@
 
 def colwidth(s):
     "Find the column width of a string for display in the local encoding"
-    return ucolwidth(s.decode(_sysstr(encoding), u'replace'))
+    return ucolwidth(s.decode(_sysstr(encoding), r'replace'))
 
 def ucolwidth(d):
     "Find the column width of a Unicode string for display"