Bug 1318488 - [mozlint] Use sys.stdout.encoding when printing formatter's output, r?smacleod
This fixes a UnicodeDecodeError when sys.stdout's encoding can't handle unicode.
MozReview-Commit-ID: 3INna8MRje5
--- a/python/mozlint/mozlint/cli.py
+++ b/python/mozlint/mozlint/cli.py
@@ -97,19 +97,19 @@ def run(paths, linters, fmt, rev, workdi
lint = LintRoller(**lintargs)
lint.read(find_linters(linters))
# run all linters
results = lint.roll(paths, rev=rev, workdir=workdir)
formatter = formatters.get(fmt)
- # Explicitly utf-8 encode the output as some of the formatters make
- # use of unicode characters. This will prevent a UnicodeEncodeError
- # on environments where utf-8 isn't the default
- print(formatter(results, failed=lint.failed).encode('utf-8', 'replace'))
+ # Encode output with 'replace' to avoid UnicodeEncodeErrors on
+ # environments that aren't using utf-8.
+ print(formatter(results, failed=lint.failed).encode(
+ sys.stdout.encoding or 'ascii', 'replace'))
return 1 if results or lint.failed else 0
if __name__ == '__main__':
parser = MozlintParser()
args = vars(parser.parse_args())
sys.exit(run(**args))