Bug 1263296 - Work around getpreferredencoding inconsistencies. r=gps
authorMike Hommey <mh+mozilla@glandium.org>
Sat, 09 Apr 2016 08:19:52 +0900
changeset 330799 9f469891dd2c686f6885ebfc6244876f4f2bb942
parent 330798 c24444a009c19e25462c7c191eb3cf4d14f2d264
child 330800 345e0864f66571aea6e6d8302bdb9b5ab5603e4c
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps
bugs1263296
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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,36 @@ 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:
+                encoding = None
+                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)