##// END OF EJS Templates
encoding: improve handling of buggy getpreferredencoding() on Mac OS X...
Dan Villiom Podlaski Christiansen -
r11892:2be70ca1 stable
parent child Browse files
Show More
@@ -8,21 +8,41 b''
8 import error
8 import error
9 import sys, unicodedata, locale, os
9 import sys, unicodedata, locale, os
10
10
11 _encodingfixup = {'646': 'ascii', 'ANSI_X3.4-1968': 'ascii'}
11 def _getpreferredencoding():
12 '''
13 On darwin, getpreferredencoding ignores the locale environment and
14 always returns mac-roman. http://bugs.python.org/issue6202 fixes this
15 for Python 2.7 and up. This is the same corrected code for earlier
16 Python versions.
17
18 However, we can't use a version check for this method, as some distributions
19 patch Python to fix this. Instead, we use it as a 'fixer' for the mac-roman
20 encoding, as it is unlikely that this encoding is the actually expected.
21 '''
22 try:
23 locale.CODESET
24 except AttributeError:
25 # Fall back to parsing environment variables :-(
26 return locale.getdefaultlocale()[1]
27
28 oldloc = locale.setlocale(locale.LC_CTYPE)
29 locale.setlocale(locale.LC_CTYPE, "")
30 result = locale.nl_langinfo(locale.CODESET)
31 locale.setlocale(locale.LC_CTYPE, oldloc)
32
33 return result
34
35 _encodingfixers = {
36 '646': lambda: 'ascii',
37 'ANSI_X3.4-1968': lambda: 'ascii',
38 'mac-roman': _getpreferredencoding
39 }
12
40
13 try:
41 try:
14 encoding = os.environ.get("HGENCODING")
42 encoding = os.environ.get("HGENCODING")
15 if sys.platform == 'darwin' and not encoding:
16 # On darwin, getpreferredencoding ignores the locale environment and
17 # always returns mac-roman. We override this if the environment is
18 # not C (has been customized by the user).
19 lc = locale.setlocale(locale.LC_CTYPE, '')
20 if lc == 'UTF-8':
21 locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8')
22 encoding = locale.getlocale()[1]
23 if not encoding:
43 if not encoding:
24 encoding = locale.getpreferredencoding() or 'ascii'
44 encoding = locale.getpreferredencoding() or 'ascii'
25 encoding = _encodingfixup.get(encoding, encoding)
45 encoding = _encodingfixers.get(encoding, lambda: encoding)()
26 except locale.Error:
46 except locale.Error:
27 encoding = 'ascii'
47 encoding = 'ascii'
28 encodingmode = os.environ.get("HGENCODINGMODE", "strict")
48 encodingmode = os.environ.get("HGENCODINGMODE", "strict")
General Comments 0
You need to be logged in to leave comments. Login now