Respect locale environment variables on darwin.
authorBrendan Cully <brendan@kublai.com>
Mon, 11 Jun 2007 12:14:31 -0700
changeset 4540 133a52d70958
parent 4539 e6c69a2491ed
child 4541 3f4555babe74
Respect locale environment variables on darwin. In python 2.4+ on darwin, locale.getpreferredencoding() returns mac-roman regardless of what LC_CTYPE, LANG etc are set to. This can produce hard-to-notice conversion errors if input text is not in mac-roman. So this patch overrides it with setlocale/getlocale if the environment has been customized, on the assumption that the user has done so deliberately.
mercurial/util.py
--- a/mercurial/util.py	Mon Jun 11 11:06:42 2007 -0700
+++ b/mercurial/util.py	Mon Jun 11 12:14:31 2007 -0700
@@ -17,8 +17,15 @@
 import os, threading, time, calendar, ConfigParser, locale, glob
 
 try:
-    _encoding = os.environ.get("HGENCODING") or locale.getpreferredencoding() \
-                or "ascii"
+    _encoding = os.environ.get("HGENCODING")
+    if sys.platform == 'darwin' and not _encoding:
+        # On darwin, getpreferredencoding ignores the locale environment and
+        # always returns mac-roman. We override this if the environment is
+        # not C (has been customized by the user).
+        locale.setlocale(locale.LC_CTYPE, '')
+        _encoding = locale.getlocale()[1]
+    if not _encoding:
+        _encoding = locale.getpreferredencoding() or 'ascii'
 except locale.Error:
     _encoding = 'ascii'
 _encodingmode = os.environ.get("HGENCODINGMODE", "strict")