diff mercurial/pycompat.py @ 31820:45761ef1bc93

py3: have registrar process docstrings in bytes Mixing bytes and unicode creates a mess. Do things in bytes as possible. New sysbytes() helper only takes care of ASCII characters, but avoids raising nasty unicode exception. This is the same design principle as sysstr().
author Yuya Nishihara <yuya@tcha.org>
date Wed, 05 Apr 2017 00:34:58 +0900
parents 8181f378b073
children c130d092042a
line wrap: on
line diff
--- a/mercurial/pycompat.py	Tue Apr 04 16:49:12 2017 +0200
+++ b/mercurial/pycompat.py	Wed Apr 05 00:34:58 2017 +0900
@@ -142,6 +142,14 @@
         """Iterate bytes as if it were a str object of Python 2"""
         return map(bytechr, s)
 
+    def sysbytes(s):
+        """Convert an internal str (e.g. keyword, __doc__) back to bytes
+
+        This never raises UnicodeEncodeError, but only ASCII characters
+        can be round-trip by sysstr(sysbytes(s)).
+        """
+        return s.encode(u'utf-8')
+
     def sysstr(s):
         """Return a keyword str to be passed to Python functions such as
         getattr() and str.encode()
@@ -210,6 +218,7 @@
     bytechr = chr
     bytestr = str
     iterbytestr = iter
+    sysbytes = identity
     sysstr = identity
 
     # Partial backport from os.py in Python 3, which only accepts bytes.