58 # preferred encoding isn't known yet; use utf-8 to avoid unicode error |
58 # preferred encoding isn't known yet; use utf-8 to avoid unicode error |
59 # and recreate it once encoding is settled |
59 # and recreate it once encoding is settled |
60 environ = dict((k.encode(u'utf-8'), v.encode(u'utf-8')) |
60 environ = dict((k.encode(u'utf-8'), v.encode(u'utf-8')) |
61 for k, v in os.environ.items()) # re-exports |
61 for k, v in os.environ.items()) # re-exports |
62 |
62 |
63 def _getpreferredencoding(): |
|
64 ''' |
|
65 On darwin, getpreferredencoding ignores the locale environment and |
|
66 always returns mac-roman. http://bugs.python.org/issue6202 fixes this |
|
67 for Python 2.7 and up. This is the same corrected code for earlier |
|
68 Python versions. |
|
69 |
|
70 However, we can't use a version check for this method, as some distributions |
|
71 patch Python to fix this. Instead, we use it as a 'fixer' for the mac-roman |
|
72 encoding, as it is unlikely that this encoding is the actually expected. |
|
73 ''' |
|
74 try: |
|
75 locale.CODESET |
|
76 except AttributeError: |
|
77 # Fall back to parsing environment variables :-( |
|
78 return locale.getdefaultlocale()[1] |
|
79 |
|
80 oldloc = locale.setlocale(locale.LC_CTYPE) |
|
81 locale.setlocale(locale.LC_CTYPE, "") |
|
82 result = locale.nl_langinfo(locale.CODESET) |
|
83 locale.setlocale(locale.LC_CTYPE, oldloc) |
|
84 |
|
85 return result |
|
86 |
|
87 _encodingfixers = { |
63 _encodingfixers = { |
88 '646': lambda: 'ascii', |
64 '646': lambda: 'ascii', |
89 'ANSI_X3.4-1968': lambda: 'ascii', |
65 'ANSI_X3.4-1968': lambda: 'ascii', |
90 'mac-roman': _getpreferredencoding |
|
91 } |
66 } |
92 |
67 |
93 try: |
68 try: |
94 encoding = environ.get("HGENCODING") |
69 encoding = environ.get("HGENCODING") |
95 if not encoding: |
70 if not encoding: |