66 # preferred encoding isn't known yet; use utf-8 to avoid unicode error |
66 # preferred encoding isn't known yet; use utf-8 to avoid unicode error |
67 # and recreate it once encoding is settled |
67 # and recreate it once encoding is settled |
68 environ = dict((k.encode(u'utf-8'), v.encode(u'utf-8')) |
68 environ = dict((k.encode(u'utf-8'), v.encode(u'utf-8')) |
69 for k, v in os.environ.items()) # re-exports |
69 for k, v in os.environ.items()) # re-exports |
70 |
70 |
71 _encodingfixers = { |
71 _encodingrewrites = { |
72 '646': lambda: 'ascii', |
72 '646': 'ascii', |
73 'ANSI_X3.4-1968': lambda: 'ascii', |
73 'ANSI_X3.4-1968': 'ascii', |
74 } |
74 } |
75 # cp65001 is a Windows variant of utf-8, which isn't supported on Python 2. |
75 # cp65001 is a Windows variant of utf-8, which isn't supported on Python 2. |
76 # No idea if it should be rewritten to the canonical name 'utf-8' on Python 3. |
76 # No idea if it should be rewritten to the canonical name 'utf-8' on Python 3. |
77 # https://bugs.python.org/issue13216 |
77 # https://bugs.python.org/issue13216 |
78 if pycompat.iswindows and not pycompat.ispy3: |
78 if pycompat.iswindows and not pycompat.ispy3: |
79 _encodingfixers['cp65001'] = lambda: 'utf-8' |
79 _encodingrewrites['cp65001'] = 'utf-8' |
80 |
80 |
81 try: |
81 try: |
82 encoding = environ.get("HGENCODING") |
82 encoding = environ.get("HGENCODING") |
83 if not encoding: |
83 if not encoding: |
84 encoding = locale.getpreferredencoding().encode('ascii') or 'ascii' |
84 encoding = locale.getpreferredencoding().encode('ascii') or 'ascii' |
85 encoding = _encodingfixers.get(encoding, lambda: encoding)() |
85 encoding = _encodingrewrites.get(encoding, encoding) |
86 except locale.Error: |
86 except locale.Error: |
87 encoding = 'ascii' |
87 encoding = 'ascii' |
88 encodingmode = environ.get("HGENCODINGMODE", "strict") |
88 encodingmode = environ.get("HGENCODINGMODE", "strict") |
89 fallbackencoding = 'ISO-8859-1' |
89 fallbackencoding = 'ISO-8859-1' |
90 |
90 |