equal
deleted
inserted
replaced
17 demandload(globals(), "cStringIO errno getpass popen2 re shutil sys tempfile") |
17 demandload(globals(), "cStringIO errno getpass popen2 re shutil sys tempfile") |
18 demandload(globals(), "os threading time calendar ConfigParser locale") |
18 demandload(globals(), "os threading time calendar ConfigParser locale") |
19 |
19 |
20 _encoding = os.environ.get("HGENCODING") or locale.getpreferredencoding() |
20 _encoding = os.environ.get("HGENCODING") or locale.getpreferredencoding() |
21 _encodingmode = os.environ.get("HGENCODINGMODE", "strict") |
21 _encodingmode = os.environ.get("HGENCODINGMODE", "strict") |
|
22 _fallbackencoding = 'ISO-8859-1' |
22 |
23 |
23 def tolocal(s): |
24 def tolocal(s): |
24 """ |
25 """ |
25 Convert a string from internal UTF-8 to local encoding |
26 Convert a string from internal UTF-8 to local encoding |
26 |
27 |
28 implementation of locale support may contain latin1 or possibly |
29 implementation of locale support may contain latin1 or possibly |
29 other character sets. We attempt to decode everything strictly |
30 other character sets. We attempt to decode everything strictly |
30 using UTF-8, then Latin-1, and failing that, we use UTF-8 and |
31 using UTF-8, then Latin-1, and failing that, we use UTF-8 and |
31 replace unknown characters. |
32 replace unknown characters. |
32 """ |
33 """ |
33 for e in "utf-8 latin1".split(): |
34 for e in ('UTF-8', _fallbackencoding): |
34 try: |
35 try: |
35 u = s.decode(e) # attempt strict decoding |
36 u = s.decode(e) # attempt strict decoding |
36 return u.encode(_encoding, "replace") |
37 return u.encode(_encoding, "replace") |
37 except UnicodeDecodeError: |
38 except UnicodeDecodeError: |
38 pass |
39 pass |