From cf2b22e35757601fb0e2e88e16c80467b9f3b500 Mon Sep 17 00:00:00 2001 From: Ricky Stewart Date: Wed, 15 Apr 2020 04:00:15 +0000 Subject: [PATCH] Bug 1628621 - Ensure whenever we write to the terminal, if ascii or charmap are the default locale, use utf-8 instead. r=glandium Differential Revision: https://phabricator.services.mozilla.com/D70429 --HG-- extra : moz-landing-system : lando --- python/mach/mach/logging.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/python/mach/mach/logging.py b/python/mach/mach/logging.py index 724e734fa8ce..306d24262c4b 100644 --- a/python/mach/mach/logging.py +++ b/python/mach/mach/logging.py @@ -13,12 +13,29 @@ try: except ImportError: blessings = None +import codecs import json import logging +import six import sys import time +# stdout and stderr may not necessarily be set up to write Unicode output, so +# reconfigure them if necessary. +def _wrap_stdstream(fh): + if fh in (sys.stderr, sys.stdout): + encoding = sys.getdefaultencoding() + encoding = 'utf-8' if encoding in ('ascii', 'charmap') else encoding + if six.PY2: + return codecs.getwriter(encoding)(sys.stdout, errors='replace') + else: + return codecs.getwriter(encoding)(sys.stdout.buffer, + errors='replace') + else: + return fh + + def format_seconds(total): """Format number of seconds to MM:SS.DD form.""" @@ -176,7 +193,8 @@ class LoggingManager(object): # Sometimes blessings fails to set up the terminal. In that case, # silently fail. try: - terminal = blessings.Terminal(stream=sys.stdout) + terminal = blessings.Terminal( + stream=_wrap_stdstream(sys.stdout)) if terminal.is_a_tty: self._terminal = terminal @@ -203,6 +221,7 @@ class LoggingManager(object): write_interval=False, write_times=True): """Enable logging to the terminal.""" + fh = _wrap_stdstream(fh) formatter = StructuredHumanFormatter(self.start_time, write_interval=write_interval, write_times=write_times)