comparison mercurial/encoding.py @ 31456:067add650129

encoding: factor out unicode variants of from/tolocal() Unfortunately, these functions will be commonly used on Python 3.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 13 Mar 2017 09:11:08 -0700
parents ce36fa9b140c
children 6419cd243017
comparison
equal deleted inserted replaced
31455:9a94239e4f12 31456:067add650129
193 except UnicodeDecodeError as inst: 193 except UnicodeDecodeError as inst:
194 sub = s[max(0, inst.start - 10):inst.start + 10] 194 sub = s[max(0, inst.start - 10):inst.start + 10]
195 raise error.Abort("decoding near '%s': %s!" % (sub, inst)) 195 raise error.Abort("decoding near '%s': %s!" % (sub, inst))
196 except LookupError as k: 196 except LookupError as k:
197 raise error.Abort(k, hint="please check your locale settings") 197 raise error.Abort(k, hint="please check your locale settings")
198
199 def unitolocal(u):
200 """Convert a unicode string to a byte string of local encoding"""
201 return tolocal(u.encode('utf-8'))
202
203 def unifromlocal(s):
204 """Convert a byte string of local encoding to a unicode string"""
205 return fromlocal(s).decode('utf-8')
198 206
199 if not _nativeenviron: 207 if not _nativeenviron:
200 # now encoding and helper functions are available, recreate the environ 208 # now encoding and helper functions are available, recreate the environ
201 # dict to be exported to other modules 209 # dict to be exported to other modules
202 environ = dict((tolocal(k.encode(u'utf-8')), tolocal(v.encode(u'utf-8'))) 210 environ = dict((tolocal(k.encode(u'utf-8')), tolocal(v.encode(u'utf-8')))