diff mercurial/encoding.py @ 33038:ce96efec8112

py3: add utility to forward __str__() to __bytes__() It calls unifromlocal() instead of sysstr() because __bytes__() may contain locale-dependent values such as paths.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 24 Jun 2017 13:48:04 +0900
parents 044f3d7eb9ae
children f5fc54e7e467
line wrap: on
line diff
--- a/mercurial/encoding.py	Sat Jun 24 13:20:30 2017 +0900
+++ b/mercurial/encoding.py	Sat Jun 24 13:48:04 2017 +0900
@@ -177,15 +177,24 @@
     """Convert a byte string of local encoding to a unicode string"""
     return fromlocal(s).decode('utf-8')
 
+def unimethod(bytesfunc):
+    """Create a proxy method that forwards __unicode__() and __str__() of
+    Python 3 to __bytes__()"""
+    def unifunc(obj):
+        return unifromlocal(bytesfunc(obj))
+    return unifunc
+
 # 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
+    strmethod = unimethod
 else:
     strtolocal = pycompat.identity
     strfromlocal = pycompat.identity
+    strmethod = pycompat.identity
 
 if not _nativeenviron:
     # now encoding and helper functions are available, recreate the environ