##// END OF EJS Templates
py3: convert encoding name and mode to str...
Yuya Nishihara -
r30033:02dbfaa6 default
parent child Browse files
Show More
@@ -17,6 +17,8 b' from . import ('
17 pycompat,
17 pycompat,
18 )
18 )
19
19
20 _sysstr = pycompat.sysstr
21
20 if pycompat.ispy3:
22 if pycompat.ispy3:
21 unichr = chr
23 unichr = chr
22
24
@@ -136,23 +138,24 b' def tolocal(s):'
136 if encoding == 'UTF-8':
138 if encoding == 'UTF-8':
137 # fast path
139 # fast path
138 return s
140 return s
139 r = u.encode(encoding, "replace")
141 r = u.encode(_sysstr(encoding), u"replace")
140 if u == r.decode(encoding):
142 if u == r.decode(_sysstr(encoding)):
141 # r is a safe, non-lossy encoding of s
143 # r is a safe, non-lossy encoding of s
142 return r
144 return r
143 return localstr(s, r)
145 return localstr(s, r)
144 except UnicodeDecodeError:
146 except UnicodeDecodeError:
145 # we should only get here if we're looking at an ancient changeset
147 # we should only get here if we're looking at an ancient changeset
146 try:
148 try:
147 u = s.decode(fallbackencoding)
149 u = s.decode(_sysstr(fallbackencoding))
148 r = u.encode(encoding, "replace")
150 r = u.encode(_sysstr(encoding), u"replace")
149 if u == r.decode(encoding):
151 if u == r.decode(_sysstr(encoding)):
150 # r is a safe, non-lossy encoding of s
152 # r is a safe, non-lossy encoding of s
151 return r
153 return r
152 return localstr(u.encode('UTF-8'), r)
154 return localstr(u.encode('UTF-8'), r)
153 except UnicodeDecodeError:
155 except UnicodeDecodeError:
154 u = s.decode("utf-8", "replace") # last ditch
156 u = s.decode("utf-8", "replace") # last ditch
155 return u.encode(encoding, "replace") # can't round-trip
157 # can't round-trip
158 return u.encode(_sysstr(encoding), u"replace")
156 except LookupError as k:
159 except LookupError as k:
157 raise error.Abort(k, hint="please check your locale settings")
160 raise error.Abort(k, hint="please check your locale settings")
158
161
@@ -172,7 +175,8 b' def fromlocal(s):'
172 return s._utf8
175 return s._utf8
173
176
174 try:
177 try:
175 return s.decode(encoding, encodingmode).encode("utf-8")
178 u = s.decode(_sysstr(encoding), _sysstr(encodingmode))
179 return u.encode("utf-8")
176 except UnicodeDecodeError as inst:
180 except UnicodeDecodeError as inst:
177 sub = s[max(0, inst.start - 10):inst.start + 10]
181 sub = s[max(0, inst.start - 10):inst.start + 10]
178 raise error.Abort("decoding near '%s': %s!" % (sub, inst))
182 raise error.Abort("decoding near '%s': %s!" % (sub, inst))
@@ -185,7 +189,7 b' wide = (os.environ.get("HGENCODINGAMBIGU'
185
189
186 def colwidth(s):
190 def colwidth(s):
187 "Find the column width of a string for display in the local encoding"
191 "Find the column width of a string for display in the local encoding"
188 return ucolwidth(s.decode(encoding, 'replace'))
192 return ucolwidth(s.decode(_sysstr(encoding), u'replace'))
189
193
190 def ucolwidth(d):
194 def ucolwidth(d):
191 "Find the column width of a Unicode string for display"
195 "Find the column width of a Unicode string for display"
@@ -265,7 +269,7 b" def trim(s, width, ellipsis='', leftside"
265 +
269 +
266 """
270 """
267 try:
271 try:
268 u = s.decode(encoding)
272 u = s.decode(_sysstr(encoding))
269 except UnicodeDecodeError:
273 except UnicodeDecodeError:
270 if len(s) <= width: # trimming is not needed
274 if len(s) <= width: # trimming is not needed
271 return s
275 return s
@@ -292,7 +296,7 b" def trim(s, width, ellipsis='', leftside"
292 for i in xrange(1, len(u)):
296 for i in xrange(1, len(u)):
293 usub = uslice(i)
297 usub = uslice(i)
294 if ucolwidth(usub) <= width:
298 if ucolwidth(usub) <= width:
295 return concat(usub.encode(encoding))
299 return concat(usub.encode(_sysstr(encoding)))
296 return ellipsis # no enough room for multi-column characters
300 return ellipsis # no enough room for multi-column characters
297
301
298 def _asciilower(s):
302 def _asciilower(s):
@@ -337,12 +341,12 b' def lower(s):'
337 if isinstance(s, localstr):
341 if isinstance(s, localstr):
338 u = s._utf8.decode("utf-8")
342 u = s._utf8.decode("utf-8")
339 else:
343 else:
340 u = s.decode(encoding, encodingmode)
344 u = s.decode(_sysstr(encoding), _sysstr(encodingmode))
341
345
342 lu = u.lower()
346 lu = u.lower()
343 if u == lu:
347 if u == lu:
344 return s # preserve localstring
348 return s # preserve localstring
345 return lu.encode(encoding)
349 return lu.encode(_sysstr(encoding))
346 except UnicodeError:
350 except UnicodeError:
347 return s.lower() # we don't know how to fold this except in ASCII
351 return s.lower() # we don't know how to fold this except in ASCII
348 except LookupError as k:
352 except LookupError as k:
@@ -360,12 +364,12 b' def upperfallback(s):'
360 if isinstance(s, localstr):
364 if isinstance(s, localstr):
361 u = s._utf8.decode("utf-8")
365 u = s._utf8.decode("utf-8")
362 else:
366 else:
363 u = s.decode(encoding, encodingmode)
367 u = s.decode(_sysstr(encoding), _sysstr(encodingmode))
364
368
365 uu = u.upper()
369 uu = u.upper()
366 if u == uu:
370 if u == uu:
367 return s # preserve localstring
371 return s # preserve localstring
368 return uu.encode(encoding)
372 return uu.encode(_sysstr(encoding))
369 except UnicodeError:
373 except UnicodeError:
370 return s.upper() # we don't know how to fold this except in ASCII
374 return s.upper() # we don't know how to fold this except in ASCII
371 except LookupError as k:
375 except LookupError as k:
General Comments 0
You need to be logged in to leave comments. Login now