Show More
@@ -1,525 +1,532 b'' | |||
|
1 | 1 | # encoding.py - character transcoding support for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | import error | |
|
9 | import unicodedata, locale, os | |
|
8 | from __future__ import absolute_import | |
|
9 | ||
|
10 | import locale | |
|
11 | import os | |
|
12 | import unicodedata | |
|
13 | ||
|
14 | from . import ( | |
|
15 | error, | |
|
16 | ) | |
|
10 | 17 | |
|
11 | 18 | # These unicode characters are ignored by HFS+ (Apple Technote 1150, |
|
12 | 19 | # "Unicode Subtleties"), so we need to ignore them in some places for |
|
13 | 20 | # sanity. |
|
14 | 21 | _ignore = [unichr(int(x, 16)).encode("utf-8") for x in |
|
15 | 22 | "200c 200d 200e 200f 202a 202b 202c 202d 202e " |
|
16 | 23 | "206a 206b 206c 206d 206e 206f feff".split()] |
|
17 | 24 | # verify the next function will work |
|
18 | 25 | assert set([i[0] for i in _ignore]) == set(["\xe2", "\xef"]) |
|
19 | 26 | |
|
20 | 27 | def hfsignoreclean(s): |
|
21 | 28 | """Remove codepoints ignored by HFS+ from s. |
|
22 | 29 | |
|
23 | 30 | >>> hfsignoreclean(u'.h\u200cg'.encode('utf-8')) |
|
24 | 31 | '.hg' |
|
25 | 32 | >>> hfsignoreclean(u'.h\ufeffg'.encode('utf-8')) |
|
26 | 33 | '.hg' |
|
27 | 34 | """ |
|
28 | 35 | if "\xe2" in s or "\xef" in s: |
|
29 | 36 | for c in _ignore: |
|
30 | 37 | s = s.replace(c, '') |
|
31 | 38 | return s |
|
32 | 39 | |
|
33 | 40 | def _getpreferredencoding(): |
|
34 | 41 | ''' |
|
35 | 42 | On darwin, getpreferredencoding ignores the locale environment and |
|
36 | 43 | always returns mac-roman. http://bugs.python.org/issue6202 fixes this |
|
37 | 44 | for Python 2.7 and up. This is the same corrected code for earlier |
|
38 | 45 | Python versions. |
|
39 | 46 | |
|
40 | 47 | However, we can't use a version check for this method, as some distributions |
|
41 | 48 | patch Python to fix this. Instead, we use it as a 'fixer' for the mac-roman |
|
42 | 49 | encoding, as it is unlikely that this encoding is the actually expected. |
|
43 | 50 | ''' |
|
44 | 51 | try: |
|
45 | 52 | locale.CODESET |
|
46 | 53 | except AttributeError: |
|
47 | 54 | # Fall back to parsing environment variables :-( |
|
48 | 55 | return locale.getdefaultlocale()[1] |
|
49 | 56 | |
|
50 | 57 | oldloc = locale.setlocale(locale.LC_CTYPE) |
|
51 | 58 | locale.setlocale(locale.LC_CTYPE, "") |
|
52 | 59 | result = locale.nl_langinfo(locale.CODESET) |
|
53 | 60 | locale.setlocale(locale.LC_CTYPE, oldloc) |
|
54 | 61 | |
|
55 | 62 | return result |
|
56 | 63 | |
|
57 | 64 | _encodingfixers = { |
|
58 | 65 | '646': lambda: 'ascii', |
|
59 | 66 | 'ANSI_X3.4-1968': lambda: 'ascii', |
|
60 | 67 | 'mac-roman': _getpreferredencoding |
|
61 | 68 | } |
|
62 | 69 | |
|
63 | 70 | try: |
|
64 | 71 | encoding = os.environ.get("HGENCODING") |
|
65 | 72 | if not encoding: |
|
66 | 73 | encoding = locale.getpreferredencoding() or 'ascii' |
|
67 | 74 | encoding = _encodingfixers.get(encoding, lambda: encoding)() |
|
68 | 75 | except locale.Error: |
|
69 | 76 | encoding = 'ascii' |
|
70 | 77 | encodingmode = os.environ.get("HGENCODINGMODE", "strict") |
|
71 | 78 | fallbackencoding = 'ISO-8859-1' |
|
72 | 79 | |
|
73 | 80 | class localstr(str): |
|
74 | 81 | '''This class allows strings that are unmodified to be |
|
75 | 82 | round-tripped to the local encoding and back''' |
|
76 | 83 | def __new__(cls, u, l): |
|
77 | 84 | s = str.__new__(cls, l) |
|
78 | 85 | s._utf8 = u |
|
79 | 86 | return s |
|
80 | 87 | def __hash__(self): |
|
81 | 88 | return hash(self._utf8) # avoid collisions in local string space |
|
82 | 89 | |
|
83 | 90 | def tolocal(s): |
|
84 | 91 | """ |
|
85 | 92 | Convert a string from internal UTF-8 to local encoding |
|
86 | 93 | |
|
87 | 94 | All internal strings should be UTF-8 but some repos before the |
|
88 | 95 | implementation of locale support may contain latin1 or possibly |
|
89 | 96 | other character sets. We attempt to decode everything strictly |
|
90 | 97 | using UTF-8, then Latin-1, and failing that, we use UTF-8 and |
|
91 | 98 | replace unknown characters. |
|
92 | 99 | |
|
93 | 100 | The localstr class is used to cache the known UTF-8 encoding of |
|
94 | 101 | strings next to their local representation to allow lossless |
|
95 | 102 | round-trip conversion back to UTF-8. |
|
96 | 103 | |
|
97 | 104 | >>> u = 'foo: \\xc3\\xa4' # utf-8 |
|
98 | 105 | >>> l = tolocal(u) |
|
99 | 106 | >>> l |
|
100 | 107 | 'foo: ?' |
|
101 | 108 | >>> fromlocal(l) |
|
102 | 109 | 'foo: \\xc3\\xa4' |
|
103 | 110 | >>> u2 = 'foo: \\xc3\\xa1' |
|
104 | 111 | >>> d = { l: 1, tolocal(u2): 2 } |
|
105 | 112 | >>> len(d) # no collision |
|
106 | 113 | 2 |
|
107 | 114 | >>> 'foo: ?' in d |
|
108 | 115 | False |
|
109 | 116 | >>> l1 = 'foo: \\xe4' # historical latin1 fallback |
|
110 | 117 | >>> l = tolocal(l1) |
|
111 | 118 | >>> l |
|
112 | 119 | 'foo: ?' |
|
113 | 120 | >>> fromlocal(l) # magically in utf-8 |
|
114 | 121 | 'foo: \\xc3\\xa4' |
|
115 | 122 | """ |
|
116 | 123 | |
|
117 | 124 | try: |
|
118 | 125 | try: |
|
119 | 126 | # make sure string is actually stored in UTF-8 |
|
120 | 127 | u = s.decode('UTF-8') |
|
121 | 128 | if encoding == 'UTF-8': |
|
122 | 129 | # fast path |
|
123 | 130 | return s |
|
124 | 131 | r = u.encode(encoding, "replace") |
|
125 | 132 | if u == r.decode(encoding): |
|
126 | 133 | # r is a safe, non-lossy encoding of s |
|
127 | 134 | return r |
|
128 | 135 | return localstr(s, r) |
|
129 | 136 | except UnicodeDecodeError: |
|
130 | 137 | # we should only get here if we're looking at an ancient changeset |
|
131 | 138 | try: |
|
132 | 139 | u = s.decode(fallbackencoding) |
|
133 | 140 | r = u.encode(encoding, "replace") |
|
134 | 141 | if u == r.decode(encoding): |
|
135 | 142 | # r is a safe, non-lossy encoding of s |
|
136 | 143 | return r |
|
137 | 144 | return localstr(u.encode('UTF-8'), r) |
|
138 | 145 | except UnicodeDecodeError: |
|
139 | 146 | u = s.decode("utf-8", "replace") # last ditch |
|
140 | 147 | return u.encode(encoding, "replace") # can't round-trip |
|
141 | 148 | except LookupError as k: |
|
142 | 149 | raise error.Abort(k, hint="please check your locale settings") |
|
143 | 150 | |
|
144 | 151 | def fromlocal(s): |
|
145 | 152 | """ |
|
146 | 153 | Convert a string from the local character encoding to UTF-8 |
|
147 | 154 | |
|
148 | 155 | We attempt to decode strings using the encoding mode set by |
|
149 | 156 | HGENCODINGMODE, which defaults to 'strict'. In this mode, unknown |
|
150 | 157 | characters will cause an error message. Other modes include |
|
151 | 158 | 'replace', which replaces unknown characters with a special |
|
152 | 159 | Unicode character, and 'ignore', which drops the character. |
|
153 | 160 | """ |
|
154 | 161 | |
|
155 | 162 | # can we do a lossless round-trip? |
|
156 | 163 | if isinstance(s, localstr): |
|
157 | 164 | return s._utf8 |
|
158 | 165 | |
|
159 | 166 | try: |
|
160 | 167 | return s.decode(encoding, encodingmode).encode("utf-8") |
|
161 | 168 | except UnicodeDecodeError as inst: |
|
162 | 169 | sub = s[max(0, inst.start - 10):inst.start + 10] |
|
163 | 170 | raise error.Abort("decoding near '%s': %s!" % (sub, inst)) |
|
164 | 171 | except LookupError as k: |
|
165 | 172 | raise error.Abort(k, hint="please check your locale settings") |
|
166 | 173 | |
|
167 | 174 | # How to treat ambiguous-width characters. Set to 'wide' to treat as wide. |
|
168 | 175 | wide = (os.environ.get("HGENCODINGAMBIGUOUS", "narrow") == "wide" |
|
169 | 176 | and "WFA" or "WF") |
|
170 | 177 | |
|
171 | 178 | def colwidth(s): |
|
172 | 179 | "Find the column width of a string for display in the local encoding" |
|
173 | 180 | return ucolwidth(s.decode(encoding, 'replace')) |
|
174 | 181 | |
|
175 | 182 | def ucolwidth(d): |
|
176 | 183 | "Find the column width of a Unicode string for display" |
|
177 | 184 | eaw = getattr(unicodedata, 'east_asian_width', None) |
|
178 | 185 | if eaw is not None: |
|
179 | 186 | return sum([eaw(c) in wide and 2 or 1 for c in d]) |
|
180 | 187 | return len(d) |
|
181 | 188 | |
|
182 | 189 | def getcols(s, start, c): |
|
183 | 190 | '''Use colwidth to find a c-column substring of s starting at byte |
|
184 | 191 | index start''' |
|
185 | 192 | for x in xrange(start + c, len(s)): |
|
186 | 193 | t = s[start:x] |
|
187 | 194 | if colwidth(t) == c: |
|
188 | 195 | return t |
|
189 | 196 | |
|
190 | 197 | def trim(s, width, ellipsis='', leftside=False): |
|
191 | 198 | """Trim string 's' to at most 'width' columns (including 'ellipsis'). |
|
192 | 199 | |
|
193 | 200 | If 'leftside' is True, left side of string 's' is trimmed. |
|
194 | 201 | 'ellipsis' is always placed at trimmed side. |
|
195 | 202 | |
|
196 | 203 | >>> ellipsis = '+++' |
|
197 |
>>> from |
|
|
204 | >>> from . import encoding | |
|
198 | 205 | >>> encoding.encoding = 'utf-8' |
|
199 | 206 | >>> t= '1234567890' |
|
200 | 207 | >>> print trim(t, 12, ellipsis=ellipsis) |
|
201 | 208 | 1234567890 |
|
202 | 209 | >>> print trim(t, 10, ellipsis=ellipsis) |
|
203 | 210 | 1234567890 |
|
204 | 211 | >>> print trim(t, 8, ellipsis=ellipsis) |
|
205 | 212 | 12345+++ |
|
206 | 213 | >>> print trim(t, 8, ellipsis=ellipsis, leftside=True) |
|
207 | 214 | +++67890 |
|
208 | 215 | >>> print trim(t, 8) |
|
209 | 216 | 12345678 |
|
210 | 217 | >>> print trim(t, 8, leftside=True) |
|
211 | 218 | 34567890 |
|
212 | 219 | >>> print trim(t, 3, ellipsis=ellipsis) |
|
213 | 220 | +++ |
|
214 | 221 | >>> print trim(t, 1, ellipsis=ellipsis) |
|
215 | 222 | + |
|
216 | 223 | >>> u = u'\u3042\u3044\u3046\u3048\u304a' # 2 x 5 = 10 columns |
|
217 | 224 | >>> t = u.encode(encoding.encoding) |
|
218 | 225 | >>> print trim(t, 12, ellipsis=ellipsis) |
|
219 | 226 | \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a |
|
220 | 227 | >>> print trim(t, 10, ellipsis=ellipsis) |
|
221 | 228 | \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a |
|
222 | 229 | >>> print trim(t, 8, ellipsis=ellipsis) |
|
223 | 230 | \xe3\x81\x82\xe3\x81\x84+++ |
|
224 | 231 | >>> print trim(t, 8, ellipsis=ellipsis, leftside=True) |
|
225 | 232 | +++\xe3\x81\x88\xe3\x81\x8a |
|
226 | 233 | >>> print trim(t, 5) |
|
227 | 234 | \xe3\x81\x82\xe3\x81\x84 |
|
228 | 235 | >>> print trim(t, 5, leftside=True) |
|
229 | 236 | \xe3\x81\x88\xe3\x81\x8a |
|
230 | 237 | >>> print trim(t, 4, ellipsis=ellipsis) |
|
231 | 238 | +++ |
|
232 | 239 | >>> print trim(t, 4, ellipsis=ellipsis, leftside=True) |
|
233 | 240 | +++ |
|
234 | 241 | >>> t = '\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa' # invalid byte sequence |
|
235 | 242 | >>> print trim(t, 12, ellipsis=ellipsis) |
|
236 | 243 | \x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa |
|
237 | 244 | >>> print trim(t, 10, ellipsis=ellipsis) |
|
238 | 245 | \x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa |
|
239 | 246 | >>> print trim(t, 8, ellipsis=ellipsis) |
|
240 | 247 | \x11\x22\x33\x44\x55+++ |
|
241 | 248 | >>> print trim(t, 8, ellipsis=ellipsis, leftside=True) |
|
242 | 249 | +++\x66\x77\x88\x99\xaa |
|
243 | 250 | >>> print trim(t, 8) |
|
244 | 251 | \x11\x22\x33\x44\x55\x66\x77\x88 |
|
245 | 252 | >>> print trim(t, 8, leftside=True) |
|
246 | 253 | \x33\x44\x55\x66\x77\x88\x99\xaa |
|
247 | 254 | >>> print trim(t, 3, ellipsis=ellipsis) |
|
248 | 255 | +++ |
|
249 | 256 | >>> print trim(t, 1, ellipsis=ellipsis) |
|
250 | 257 | + |
|
251 | 258 | """ |
|
252 | 259 | try: |
|
253 | 260 | u = s.decode(encoding) |
|
254 | 261 | except UnicodeDecodeError: |
|
255 | 262 | if len(s) <= width: # trimming is not needed |
|
256 | 263 | return s |
|
257 | 264 | width -= len(ellipsis) |
|
258 | 265 | if width <= 0: # no enough room even for ellipsis |
|
259 | 266 | return ellipsis[:width + len(ellipsis)] |
|
260 | 267 | if leftside: |
|
261 | 268 | return ellipsis + s[-width:] |
|
262 | 269 | return s[:width] + ellipsis |
|
263 | 270 | |
|
264 | 271 | if ucolwidth(u) <= width: # trimming is not needed |
|
265 | 272 | return s |
|
266 | 273 | |
|
267 | 274 | width -= len(ellipsis) |
|
268 | 275 | if width <= 0: # no enough room even for ellipsis |
|
269 | 276 | return ellipsis[:width + len(ellipsis)] |
|
270 | 277 | |
|
271 | 278 | if leftside: |
|
272 | 279 | uslice = lambda i: u[i:] |
|
273 | 280 | concat = lambda s: ellipsis + s |
|
274 | 281 | else: |
|
275 | 282 | uslice = lambda i: u[:-i] |
|
276 | 283 | concat = lambda s: s + ellipsis |
|
277 | 284 | for i in xrange(1, len(u)): |
|
278 | 285 | usub = uslice(i) |
|
279 | 286 | if ucolwidth(usub) <= width: |
|
280 | 287 | return concat(usub.encode(encoding)) |
|
281 | 288 | return ellipsis # no enough room for multi-column characters |
|
282 | 289 | |
|
283 | 290 | def _asciilower(s): |
|
284 | 291 | '''convert a string to lowercase if ASCII |
|
285 | 292 | |
|
286 | 293 | Raises UnicodeDecodeError if non-ASCII characters are found.''' |
|
287 | 294 | s.decode('ascii') |
|
288 | 295 | return s.lower() |
|
289 | 296 | |
|
290 | 297 | def asciilower(s): |
|
291 | 298 | # delay importing avoids cyclic dependency around "parsers" in |
|
292 | 299 | # pure Python build (util => i18n => encoding => parsers => util) |
|
293 | import parsers | |
|
300 | from . import parsers | |
|
294 | 301 | impl = getattr(parsers, 'asciilower', _asciilower) |
|
295 | 302 | global asciilower |
|
296 | 303 | asciilower = impl |
|
297 | 304 | return impl(s) |
|
298 | 305 | |
|
299 | 306 | def _asciiupper(s): |
|
300 | 307 | '''convert a string to uppercase if ASCII |
|
301 | 308 | |
|
302 | 309 | Raises UnicodeDecodeError if non-ASCII characters are found.''' |
|
303 | 310 | s.decode('ascii') |
|
304 | 311 | return s.upper() |
|
305 | 312 | |
|
306 | 313 | def asciiupper(s): |
|
307 | 314 | # delay importing avoids cyclic dependency around "parsers" in |
|
308 | 315 | # pure Python build (util => i18n => encoding => parsers => util) |
|
309 | import parsers | |
|
316 | from . import parsers | |
|
310 | 317 | impl = getattr(parsers, 'asciiupper', _asciiupper) |
|
311 | 318 | global asciiupper |
|
312 | 319 | asciiupper = impl |
|
313 | 320 | return impl(s) |
|
314 | 321 | |
|
315 | 322 | def lower(s): |
|
316 | 323 | "best-effort encoding-aware case-folding of local string s" |
|
317 | 324 | try: |
|
318 | 325 | return asciilower(s) |
|
319 | 326 | except UnicodeDecodeError: |
|
320 | 327 | pass |
|
321 | 328 | try: |
|
322 | 329 | if isinstance(s, localstr): |
|
323 | 330 | u = s._utf8.decode("utf-8") |
|
324 | 331 | else: |
|
325 | 332 | u = s.decode(encoding, encodingmode) |
|
326 | 333 | |
|
327 | 334 | lu = u.lower() |
|
328 | 335 | if u == lu: |
|
329 | 336 | return s # preserve localstring |
|
330 | 337 | return lu.encode(encoding) |
|
331 | 338 | except UnicodeError: |
|
332 | 339 | return s.lower() # we don't know how to fold this except in ASCII |
|
333 | 340 | except LookupError as k: |
|
334 | 341 | raise error.Abort(k, hint="please check your locale settings") |
|
335 | 342 | |
|
336 | 343 | def upper(s): |
|
337 | 344 | "best-effort encoding-aware case-folding of local string s" |
|
338 | 345 | try: |
|
339 | 346 | return asciiupper(s) |
|
340 | 347 | except UnicodeDecodeError: |
|
341 | 348 | return upperfallback(s) |
|
342 | 349 | |
|
343 | 350 | def upperfallback(s): |
|
344 | 351 | try: |
|
345 | 352 | if isinstance(s, localstr): |
|
346 | 353 | u = s._utf8.decode("utf-8") |
|
347 | 354 | else: |
|
348 | 355 | u = s.decode(encoding, encodingmode) |
|
349 | 356 | |
|
350 | 357 | uu = u.upper() |
|
351 | 358 | if u == uu: |
|
352 | 359 | return s # preserve localstring |
|
353 | 360 | return uu.encode(encoding) |
|
354 | 361 | except UnicodeError: |
|
355 | 362 | return s.upper() # we don't know how to fold this except in ASCII |
|
356 | 363 | except LookupError as k: |
|
357 | 364 | raise error.Abort(k, hint="please check your locale settings") |
|
358 | 365 | |
|
359 | 366 | class normcasespecs(object): |
|
360 | 367 | '''what a platform's normcase does to ASCII strings |
|
361 | 368 | |
|
362 | 369 | This is specified per platform, and should be consistent with what normcase |
|
363 | 370 | on that platform actually does. |
|
364 | 371 | |
|
365 | 372 | lower: normcase lowercases ASCII strings |
|
366 | 373 | upper: normcase uppercases ASCII strings |
|
367 | 374 | other: the fallback function should always be called |
|
368 | 375 | |
|
369 | 376 | This should be kept in sync with normcase_spec in util.h.''' |
|
370 | 377 | lower = -1 |
|
371 | 378 | upper = 1 |
|
372 | 379 | other = 0 |
|
373 | 380 | |
|
374 | 381 | _jsonmap = {} |
|
375 | 382 | |
|
376 | 383 | def jsonescape(s): |
|
377 | 384 | '''returns a string suitable for JSON |
|
378 | 385 | |
|
379 | 386 | JSON is problematic for us because it doesn't support non-Unicode |
|
380 | 387 | bytes. To deal with this, we take the following approach: |
|
381 | 388 | |
|
382 | 389 | - localstr objects are converted back to UTF-8 |
|
383 | 390 | - valid UTF-8/ASCII strings are passed as-is |
|
384 | 391 | - other strings are converted to UTF-8b surrogate encoding |
|
385 | 392 | - apply JSON-specified string escaping |
|
386 | 393 | |
|
387 | 394 | (escapes are doubled in these tests) |
|
388 | 395 | |
|
389 | 396 | >>> jsonescape('this is a test') |
|
390 | 397 | 'this is a test' |
|
391 | 398 | >>> jsonescape('escape characters: \\0 \\x0b \\t \\n \\r \\" \\\\') |
|
392 | 399 | 'escape characters: \\\\u0000 \\\\u000b \\\\t \\\\n \\\\r \\\\" \\\\\\\\' |
|
393 | 400 | >>> jsonescape('a weird byte: \\xdd') |
|
394 | 401 | 'a weird byte: \\xed\\xb3\\x9d' |
|
395 | 402 | >>> jsonescape('utf-8: caf\\xc3\\xa9') |
|
396 | 403 | 'utf-8: caf\\xc3\\xa9' |
|
397 | 404 | >>> jsonescape('') |
|
398 | 405 | '' |
|
399 | 406 | ''' |
|
400 | 407 | |
|
401 | 408 | if not _jsonmap: |
|
402 | 409 | for x in xrange(32): |
|
403 | 410 | _jsonmap[chr(x)] = "\u%04x" %x |
|
404 | 411 | for x in xrange(32, 256): |
|
405 | 412 | c = chr(x) |
|
406 | 413 | _jsonmap[c] = c |
|
407 | 414 | _jsonmap['\t'] = '\\t' |
|
408 | 415 | _jsonmap['\n'] = '\\n' |
|
409 | 416 | _jsonmap['\"'] = '\\"' |
|
410 | 417 | _jsonmap['\\'] = '\\\\' |
|
411 | 418 | _jsonmap['\b'] = '\\b' |
|
412 | 419 | _jsonmap['\f'] = '\\f' |
|
413 | 420 | _jsonmap['\r'] = '\\r' |
|
414 | 421 | |
|
415 | 422 | return ''.join(_jsonmap[c] for c in toutf8b(s)) |
|
416 | 423 | |
|
417 | 424 | _utf8len = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4] |
|
418 | 425 | |
|
419 | 426 | def getutf8char(s, pos): |
|
420 | 427 | '''get the next full utf-8 character in the given string, starting at pos |
|
421 | 428 | |
|
422 | 429 | Raises a UnicodeError if the given location does not start a valid |
|
423 | 430 | utf-8 character. |
|
424 | 431 | ''' |
|
425 | 432 | |
|
426 | 433 | # find how many bytes to attempt decoding from first nibble |
|
427 | 434 | l = _utf8len[ord(s[pos]) >> 4] |
|
428 | 435 | if not l: # ascii |
|
429 | 436 | return s[pos] |
|
430 | 437 | |
|
431 | 438 | c = s[pos:pos + l] |
|
432 | 439 | # validate with attempted decode |
|
433 | 440 | c.decode("utf-8") |
|
434 | 441 | return c |
|
435 | 442 | |
|
436 | 443 | def toutf8b(s): |
|
437 | 444 | '''convert a local, possibly-binary string into UTF-8b |
|
438 | 445 | |
|
439 | 446 | This is intended as a generic method to preserve data when working |
|
440 | 447 | with schemes like JSON and XML that have no provision for |
|
441 | 448 | arbitrary byte strings. As Mercurial often doesn't know |
|
442 | 449 | what encoding data is in, we use so-called UTF-8b. |
|
443 | 450 | |
|
444 | 451 | If a string is already valid UTF-8 (or ASCII), it passes unmodified. |
|
445 | 452 | Otherwise, unsupported bytes are mapped to UTF-16 surrogate range, |
|
446 | 453 | uDC00-uDCFF. |
|
447 | 454 | |
|
448 | 455 | Principles of operation: |
|
449 | 456 | |
|
450 | 457 | - ASCII and UTF-8 data successfully round-trips and is understood |
|
451 | 458 | by Unicode-oriented clients |
|
452 | 459 | - filenames and file contents in arbitrary other encodings can have |
|
453 | 460 | be round-tripped or recovered by clueful clients |
|
454 | 461 | - local strings that have a cached known UTF-8 encoding (aka |
|
455 | 462 | localstr) get sent as UTF-8 so Unicode-oriented clients get the |
|
456 | 463 | Unicode data they want |
|
457 | 464 | - because we must preserve UTF-8 bytestring in places such as |
|
458 | 465 | filenames, metadata can't be roundtripped without help |
|
459 | 466 | |
|
460 | 467 | (Note: "UTF-8b" often refers to decoding a mix of valid UTF-8 and |
|
461 | 468 | arbitrary bytes into an internal Unicode format that can be |
|
462 | 469 | re-encoded back into the original. Here we are exposing the |
|
463 | 470 | internal surrogate encoding as a UTF-8 string.) |
|
464 | 471 | ''' |
|
465 | 472 | |
|
466 | 473 | if "\xed" not in s: |
|
467 | 474 | if isinstance(s, localstr): |
|
468 | 475 | return s._utf8 |
|
469 | 476 | try: |
|
470 | 477 | s.decode('utf-8') |
|
471 | 478 | return s |
|
472 | 479 | except UnicodeDecodeError: |
|
473 | 480 | pass |
|
474 | 481 | |
|
475 | 482 | r = "" |
|
476 | 483 | pos = 0 |
|
477 | 484 | l = len(s) |
|
478 | 485 | while pos < l: |
|
479 | 486 | try: |
|
480 | 487 | c = getutf8char(s, pos) |
|
481 | 488 | if "\xed\xb0\x80" <= c <= "\xed\xb3\xbf": |
|
482 | 489 | # have to re-escape existing U+DCxx characters |
|
483 | 490 | c = unichr(0xdc00 + ord(s[pos])).encode('utf-8') |
|
484 | 491 | pos += 1 |
|
485 | 492 | else: |
|
486 | 493 | pos += len(c) |
|
487 | 494 | except UnicodeDecodeError: |
|
488 | 495 | c = unichr(0xdc00 + ord(s[pos])).encode('utf-8') |
|
489 | 496 | pos += 1 |
|
490 | 497 | r += c |
|
491 | 498 | return r |
|
492 | 499 | |
|
493 | 500 | def fromutf8b(s): |
|
494 | 501 | '''Given a UTF-8b string, return a local, possibly-binary string. |
|
495 | 502 | |
|
496 | 503 | return the original binary string. This |
|
497 | 504 | is a round-trip process for strings like filenames, but metadata |
|
498 | 505 | that's was passed through tolocal will remain in UTF-8. |
|
499 | 506 | |
|
500 | 507 | >>> roundtrip = lambda x: fromutf8b(toutf8b(x)) == x |
|
501 | 508 | >>> m = "\\xc3\\xa9\\x99abcd" |
|
502 | 509 | >>> toutf8b(m) |
|
503 | 510 | '\\xc3\\xa9\\xed\\xb2\\x99abcd' |
|
504 | 511 | >>> roundtrip(m) |
|
505 | 512 | True |
|
506 | 513 | >>> roundtrip("\\xc2\\xc2\\x80") |
|
507 | 514 | True |
|
508 | 515 | >>> roundtrip("\\xef\\xbf\\xbd") |
|
509 | 516 | True |
|
510 | 517 | >>> roundtrip("\\xef\\xef\\xbf\\xbd") |
|
511 | 518 | True |
|
512 | 519 | ''' |
|
513 | 520 | |
|
514 | 521 | # fast path - look for uDxxx prefixes in s |
|
515 | 522 | if "\xed" not in s: |
|
516 | 523 | return s |
|
517 | 524 | |
|
518 | 525 | u = s.decode("utf-8") |
|
519 | 526 | r = "" |
|
520 | 527 | for c in u: |
|
521 | 528 | if ord(c) & 0xffff00 == 0xdc00: |
|
522 | 529 | r += chr(ord(c) & 0xff) |
|
523 | 530 | else: |
|
524 | 531 | r += c.encode("utf-8") |
|
525 | 532 | return r |
@@ -1,212 +1,211 b'' | |||
|
1 | 1 | #require test-repo |
|
2 | 2 | |
|
3 | 3 | $ cd "$TESTDIR"/.. |
|
4 | 4 | |
|
5 | 5 | $ hg files 'set:(**.py)' | xargs python contrib/check-py3-compat.py |
|
6 | 6 | contrib/casesmash.py not using absolute_import |
|
7 | 7 | contrib/check-code.py not using absolute_import |
|
8 | 8 | contrib/check-code.py requires print_function |
|
9 | 9 | contrib/check-config.py not using absolute_import |
|
10 | 10 | contrib/check-config.py requires print_function |
|
11 | 11 | contrib/debugcmdserver.py not using absolute_import |
|
12 | 12 | contrib/debugcmdserver.py requires print_function |
|
13 | 13 | contrib/debugshell.py not using absolute_import |
|
14 | 14 | contrib/fixpax.py not using absolute_import |
|
15 | 15 | contrib/fixpax.py requires print_function |
|
16 | 16 | contrib/hgclient.py not using absolute_import |
|
17 | 17 | contrib/hgclient.py requires print_function |
|
18 | 18 | contrib/hgfixes/fix_bytes.py not using absolute_import |
|
19 | 19 | contrib/hgfixes/fix_bytesmod.py not using absolute_import |
|
20 | 20 | contrib/hgfixes/fix_leftover_imports.py not using absolute_import |
|
21 | 21 | contrib/import-checker.py not using absolute_import |
|
22 | 22 | contrib/import-checker.py requires print_function |
|
23 | 23 | contrib/memory.py not using absolute_import |
|
24 | 24 | contrib/perf.py not using absolute_import |
|
25 | 25 | contrib/python-hook-examples.py not using absolute_import |
|
26 | 26 | contrib/revsetbenchmarks.py not using absolute_import |
|
27 | 27 | contrib/revsetbenchmarks.py requires print_function |
|
28 | 28 | contrib/showstack.py not using absolute_import |
|
29 | 29 | contrib/synthrepo.py not using absolute_import |
|
30 | 30 | contrib/win32/hgwebdir_wsgi.py not using absolute_import |
|
31 | 31 | doc/check-seclevel.py not using absolute_import |
|
32 | 32 | doc/gendoc.py not using absolute_import |
|
33 | 33 | doc/hgmanpage.py not using absolute_import |
|
34 | 34 | hgext/__init__.py not using absolute_import |
|
35 | 35 | hgext/acl.py not using absolute_import |
|
36 | 36 | hgext/blackbox.py not using absolute_import |
|
37 | 37 | hgext/bugzilla.py not using absolute_import |
|
38 | 38 | hgext/censor.py not using absolute_import |
|
39 | 39 | hgext/children.py not using absolute_import |
|
40 | 40 | hgext/churn.py not using absolute_import |
|
41 | 41 | hgext/clonebundles.py not using absolute_import |
|
42 | 42 | hgext/color.py not using absolute_import |
|
43 | 43 | hgext/convert/__init__.py not using absolute_import |
|
44 | 44 | hgext/convert/bzr.py not using absolute_import |
|
45 | 45 | hgext/convert/common.py not using absolute_import |
|
46 | 46 | hgext/convert/convcmd.py not using absolute_import |
|
47 | 47 | hgext/convert/cvs.py not using absolute_import |
|
48 | 48 | hgext/convert/cvsps.py not using absolute_import |
|
49 | 49 | hgext/convert/darcs.py not using absolute_import |
|
50 | 50 | hgext/convert/filemap.py not using absolute_import |
|
51 | 51 | hgext/convert/git.py not using absolute_import |
|
52 | 52 | hgext/convert/gnuarch.py not using absolute_import |
|
53 | 53 | hgext/convert/hg.py not using absolute_import |
|
54 | 54 | hgext/convert/monotone.py not using absolute_import |
|
55 | 55 | hgext/convert/p4.py not using absolute_import |
|
56 | 56 | hgext/convert/subversion.py not using absolute_import |
|
57 | 57 | hgext/convert/transport.py not using absolute_import |
|
58 | 58 | hgext/eol.py not using absolute_import |
|
59 | 59 | hgext/extdiff.py not using absolute_import |
|
60 | 60 | hgext/factotum.py not using absolute_import |
|
61 | 61 | hgext/fetch.py not using absolute_import |
|
62 | 62 | hgext/gpg.py not using absolute_import |
|
63 | 63 | hgext/graphlog.py not using absolute_import |
|
64 | 64 | hgext/hgcia.py not using absolute_import |
|
65 | 65 | hgext/hgk.py not using absolute_import |
|
66 | 66 | hgext/highlight/__init__.py not using absolute_import |
|
67 | 67 | hgext/highlight/highlight.py not using absolute_import |
|
68 | 68 | hgext/histedit.py not using absolute_import |
|
69 | 69 | hgext/keyword.py not using absolute_import |
|
70 | 70 | hgext/largefiles/__init__.py not using absolute_import |
|
71 | 71 | hgext/largefiles/basestore.py not using absolute_import |
|
72 | 72 | hgext/largefiles/lfcommands.py not using absolute_import |
|
73 | 73 | hgext/largefiles/lfutil.py not using absolute_import |
|
74 | 74 | hgext/largefiles/localstore.py not using absolute_import |
|
75 | 75 | hgext/largefiles/overrides.py not using absolute_import |
|
76 | 76 | hgext/largefiles/proto.py not using absolute_import |
|
77 | 77 | hgext/largefiles/remotestore.py not using absolute_import |
|
78 | 78 | hgext/largefiles/reposetup.py not using absolute_import |
|
79 | 79 | hgext/largefiles/uisetup.py not using absolute_import |
|
80 | 80 | hgext/largefiles/wirestore.py not using absolute_import |
|
81 | 81 | hgext/mq.py not using absolute_import |
|
82 | 82 | hgext/notify.py not using absolute_import |
|
83 | 83 | hgext/pager.py not using absolute_import |
|
84 | 84 | hgext/patchbomb.py not using absolute_import |
|
85 | 85 | hgext/purge.py not using absolute_import |
|
86 | 86 | hgext/rebase.py not using absolute_import |
|
87 | 87 | hgext/record.py not using absolute_import |
|
88 | 88 | hgext/relink.py not using absolute_import |
|
89 | 89 | hgext/schemes.py not using absolute_import |
|
90 | 90 | hgext/share.py not using absolute_import |
|
91 | 91 | hgext/shelve.py not using absolute_import |
|
92 | 92 | hgext/strip.py not using absolute_import |
|
93 | 93 | hgext/transplant.py not using absolute_import |
|
94 | 94 | hgext/win32mbcs.py not using absolute_import |
|
95 | 95 | hgext/win32text.py not using absolute_import |
|
96 | 96 | hgext/zeroconf/Zeroconf.py not using absolute_import |
|
97 | 97 | hgext/zeroconf/Zeroconf.py requires print_function |
|
98 | 98 | hgext/zeroconf/__init__.py not using absolute_import |
|
99 | 99 | i18n/check-translation.py not using absolute_import |
|
100 | 100 | i18n/polib.py not using absolute_import |
|
101 | 101 | mercurial/byterange.py not using absolute_import |
|
102 | 102 | mercurial/cmdutil.py not using absolute_import |
|
103 | 103 | mercurial/commands.py not using absolute_import |
|
104 | 104 | mercurial/context.py not using absolute_import |
|
105 | 105 | mercurial/dirstate.py not using absolute_import |
|
106 | 106 | mercurial/dispatch.py requires print_function |
|
107 | mercurial/encoding.py not using absolute_import | |
|
108 | 107 | mercurial/exchange.py not using absolute_import |
|
109 | 108 | mercurial/help.py not using absolute_import |
|
110 | 109 | mercurial/httpclient/__init__.py not using absolute_import |
|
111 | 110 | mercurial/httpclient/_readers.py not using absolute_import |
|
112 | 111 | mercurial/httpclient/socketutil.py not using absolute_import |
|
113 | 112 | mercurial/httpconnection.py not using absolute_import |
|
114 | 113 | mercurial/keepalive.py not using absolute_import |
|
115 | 114 | mercurial/keepalive.py requires print_function |
|
116 | 115 | mercurial/localrepo.py not using absolute_import |
|
117 | 116 | mercurial/lsprof.py requires print_function |
|
118 | 117 | mercurial/lsprofcalltree.py not using absolute_import |
|
119 | 118 | mercurial/lsprofcalltree.py requires print_function |
|
120 | 119 | mercurial/mail.py requires print_function |
|
121 | 120 | mercurial/manifest.py not using absolute_import |
|
122 | 121 | mercurial/mdiff.py not using absolute_import |
|
123 | 122 | mercurial/patch.py not using absolute_import |
|
124 | 123 | mercurial/pvec.py not using absolute_import |
|
125 | 124 | mercurial/py3kcompat.py not using absolute_import |
|
126 | 125 | mercurial/revlog.py not using absolute_import |
|
127 | 126 | mercurial/scmposix.py not using absolute_import |
|
128 | 127 | mercurial/scmutil.py not using absolute_import |
|
129 | 128 | mercurial/scmwindows.py not using absolute_import |
|
130 | 129 | mercurial/similar.py not using absolute_import |
|
131 | 130 | mercurial/store.py not using absolute_import |
|
132 | 131 | mercurial/util.py not using absolute_import |
|
133 | 132 | mercurial/windows.py not using absolute_import |
|
134 | 133 | setup.py not using absolute_import |
|
135 | 134 | tests/filterpyflakes.py requires print_function |
|
136 | 135 | tests/generate-working-copy-states.py requires print_function |
|
137 | 136 | tests/get-with-headers.py requires print_function |
|
138 | 137 | tests/heredoctest.py requires print_function |
|
139 | 138 | tests/hypothesishelpers.py not using absolute_import |
|
140 | 139 | tests/hypothesishelpers.py requires print_function |
|
141 | 140 | tests/killdaemons.py not using absolute_import |
|
142 | 141 | tests/md5sum.py not using absolute_import |
|
143 | 142 | tests/mockblackbox.py not using absolute_import |
|
144 | 143 | tests/printenv.py not using absolute_import |
|
145 | 144 | tests/readlink.py not using absolute_import |
|
146 | 145 | tests/readlink.py requires print_function |
|
147 | 146 | tests/revlog-formatv0.py not using absolute_import |
|
148 | 147 | tests/run-tests.py not using absolute_import |
|
149 | 148 | tests/seq.py not using absolute_import |
|
150 | 149 | tests/seq.py requires print_function |
|
151 | 150 | tests/silenttestrunner.py not using absolute_import |
|
152 | 151 | tests/silenttestrunner.py requires print_function |
|
153 | 152 | tests/sitecustomize.py not using absolute_import |
|
154 | 153 | tests/svn-safe-append.py not using absolute_import |
|
155 | 154 | tests/svnxml.py not using absolute_import |
|
156 | 155 | tests/test-ancestor.py requires print_function |
|
157 | 156 | tests/test-atomictempfile.py not using absolute_import |
|
158 | 157 | tests/test-batching.py not using absolute_import |
|
159 | 158 | tests/test-batching.py requires print_function |
|
160 | 159 | tests/test-bdiff.py not using absolute_import |
|
161 | 160 | tests/test-bdiff.py requires print_function |
|
162 | 161 | tests/test-context.py not using absolute_import |
|
163 | 162 | tests/test-context.py requires print_function |
|
164 | 163 | tests/test-demandimport.py not using absolute_import |
|
165 | 164 | tests/test-demandimport.py requires print_function |
|
166 | 165 | tests/test-dispatch.py not using absolute_import |
|
167 | 166 | tests/test-dispatch.py requires print_function |
|
168 | 167 | tests/test-doctest.py not using absolute_import |
|
169 | 168 | tests/test-duplicateoptions.py not using absolute_import |
|
170 | 169 | tests/test-duplicateoptions.py requires print_function |
|
171 | 170 | tests/test-filecache.py not using absolute_import |
|
172 | 171 | tests/test-filecache.py requires print_function |
|
173 | 172 | tests/test-filelog.py not using absolute_import |
|
174 | 173 | tests/test-filelog.py requires print_function |
|
175 | 174 | tests/test-hg-parseurl.py not using absolute_import |
|
176 | 175 | tests/test-hg-parseurl.py requires print_function |
|
177 | 176 | tests/test-hgweb-auth.py not using absolute_import |
|
178 | 177 | tests/test-hgweb-auth.py requires print_function |
|
179 | 178 | tests/test-hgwebdir-paths.py not using absolute_import |
|
180 | 179 | tests/test-hybridencode.py not using absolute_import |
|
181 | 180 | tests/test-hybridencode.py requires print_function |
|
182 | 181 | tests/test-lrucachedict.py not using absolute_import |
|
183 | 182 | tests/test-lrucachedict.py requires print_function |
|
184 | 183 | tests/test-manifest.py not using absolute_import |
|
185 | 184 | tests/test-minirst.py not using absolute_import |
|
186 | 185 | tests/test-minirst.py requires print_function |
|
187 | 186 | tests/test-parseindex2.py not using absolute_import |
|
188 | 187 | tests/test-parseindex2.py requires print_function |
|
189 | 188 | tests/test-pathencode.py not using absolute_import |
|
190 | 189 | tests/test-pathencode.py requires print_function |
|
191 | 190 | tests/test-propertycache.py not using absolute_import |
|
192 | 191 | tests/test-propertycache.py requires print_function |
|
193 | 192 | tests/test-revlog-ancestry.py not using absolute_import |
|
194 | 193 | tests/test-revlog-ancestry.py requires print_function |
|
195 | 194 | tests/test-run-tests.py not using absolute_import |
|
196 | 195 | tests/test-simplemerge.py not using absolute_import |
|
197 | 196 | tests/test-status-inprocess.py not using absolute_import |
|
198 | 197 | tests/test-status-inprocess.py requires print_function |
|
199 | 198 | tests/test-symlink-os-yes-fs-no.py not using absolute_import |
|
200 | 199 | tests/test-trusted.py not using absolute_import |
|
201 | 200 | tests/test-trusted.py requires print_function |
|
202 | 201 | tests/test-ui-color.py not using absolute_import |
|
203 | 202 | tests/test-ui-color.py requires print_function |
|
204 | 203 | tests/test-ui-config.py not using absolute_import |
|
205 | 204 | tests/test-ui-config.py requires print_function |
|
206 | 205 | tests/test-ui-verbosity.py not using absolute_import |
|
207 | 206 | tests/test-ui-verbosity.py requires print_function |
|
208 | 207 | tests/test-url.py not using absolute_import |
|
209 | 208 | tests/test-url.py requires print_function |
|
210 | 209 | tests/test-walkrepo.py requires print_function |
|
211 | 210 | tests/test-wireproto.py requires print_function |
|
212 | 211 | tests/tinyproxy.py requires print_function |
General Comments 0
You need to be logged in to leave comments.
Login now