Mercurial > public > mercurial-scm > hg
comparison mercurial/encoding.py @ 33022: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 |
comparison
equal
deleted
inserted
replaced
33021:24c0a9a7fa86 | 33022:ce96efec8112 |
---|---|
175 | 175 |
176 def unifromlocal(s): | 176 def unifromlocal(s): |
177 """Convert a byte string of local encoding to a unicode string""" | 177 """Convert a byte string of local encoding to a unicode string""" |
178 return fromlocal(s).decode('utf-8') | 178 return fromlocal(s).decode('utf-8') |
179 | 179 |
180 def unimethod(bytesfunc): | |
181 """Create a proxy method that forwards __unicode__() and __str__() of | |
182 Python 3 to __bytes__()""" | |
183 def unifunc(obj): | |
184 return unifromlocal(bytesfunc(obj)) | |
185 return unifunc | |
186 | |
180 # converter functions between native str and byte string. use these if the | 187 # converter functions between native str and byte string. use these if the |
181 # character encoding is not aware (e.g. exception message) or is known to | 188 # character encoding is not aware (e.g. exception message) or is known to |
182 # be locale dependent (e.g. date formatting.) | 189 # be locale dependent (e.g. date formatting.) |
183 if pycompat.ispy3: | 190 if pycompat.ispy3: |
184 strtolocal = unitolocal | 191 strtolocal = unitolocal |
185 strfromlocal = unifromlocal | 192 strfromlocal = unifromlocal |
193 strmethod = unimethod | |
186 else: | 194 else: |
187 strtolocal = pycompat.identity | 195 strtolocal = pycompat.identity |
188 strfromlocal = pycompat.identity | 196 strfromlocal = pycompat.identity |
197 strmethod = pycompat.identity | |
189 | 198 |
190 if not _nativeenviron: | 199 if not _nativeenviron: |
191 # now encoding and helper functions are available, recreate the environ | 200 # now encoding and helper functions are available, recreate the environ |
192 # dict to be exported to other modules | 201 # dict to be exported to other modules |
193 environ = dict((tolocal(k.encode(u'utf-8')), tolocal(v.encode(u'utf-8'))) | 202 environ = dict((tolocal(k.encode(u'utf-8')), tolocal(v.encode(u'utf-8'))) |