Bug 1263296 - Work around getpreferredencoding inconsistencies. r?gps draft
authorMike Hommey <mh+mozilla@glandium.org>
Sat, 09 Apr 2016 08:19:52 +0900
changeset 349122 a1c60f66f500b033f5091415f904ab87b2ca1303
parent 348820 b5782fbaae23dbd072d2261a6a16dfd485956b35
child 349600 b1f6d9b41d1210751d94e43ae0255fdbb2b3aa5d
push id14994
push userbmo:mh+mozilla@glandium.org
push dateFri, 08 Apr 2016 23:27:39 +0000
reviewersgps
bugs1263296
milestone48.0a1
Bug 1263296 - Work around getpreferredencoding inconsistencies. r?gps Whether it uses locale._parse_localename or nl_langinfo makes it have completely different results in weird and/or widespread locale settings (LC_ALL=UTF-8 or LC_ALL=C).
python/mozbuild/mozbuild/configure/util.py
--- a/python/mozbuild/mozbuild/configure/util.py
+++ b/python/mozbuild/mozbuild/configure/util.py
@@ -60,20 +60,35 @@ class ConfigureOutputHandler(logging.Han
     '''
     def __init__(self, stdout=sys.stdout, stderr=sys.stderr, maxlen=20):
         super(ConfigureOutputHandler, self).__init__()
 
         # Python has this feature where it sets the encoding of pipes to
         # ascii, which blatantly fails when trying to print out non-ascii.
         def fix_encoding(fh):
             try:
-                if not fh.isatty():
-                    return codecs.getwriter(locale.getpreferredencoding())(fh)
+                isatty = fh.isatty()
             except AttributeError:
-                pass
+                isatty = True
+
+            if not isatty:
+                try:
+                    encoding = locale.getpreferredencoding()
+                except ValueError:
+                    # On english OSX, LC_ALL is UTF-8 (not en-US.UTF-8), and
+                    # that throws off locale._parse_localename, which ends up
+                    # being used on e.g. homebrew python.
+                    if os.environ.get('LC_ALL', '').upper() == 'UTF-8':
+                        encoding = 'utf-8'
+
+                # locale._parse_localename makes locale.getpreferredencoding
+                # return None when LC_ALL is C, instead of e.g. 'US-ASCII' or
+                # 'ANSI_X3.4-1968' when it uses nl_langinfo.
+                if encoding:
+                    return codecs.getwriter(encoding)(fh)
             return fh
 
         self._stdout = fix_encoding(stdout)
         self._stderr = fix_encoding(stderr) if stdout != stderr else self._stdout
         try:
             fd1 = self._stdout.fileno()
             fd2 = self._stderr.fileno()
             self._same_output = self._is_same_output(fd1, fd2)