Bug 1318488 - [mozlint] Use sys.stdout.encoding when printing formatter's output, r?smacleod draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Thu, 17 Nov 2016 17:36:34 -0500
changeset 442132 ed32a967e6086eac84a1c5fa0017ebcce58c4b3b
parent 442068 0534254e9a40b4bade2577c631fe4cfa0b5db41d
child 537706 b01f5f47439fb2d4e7bd4b0ad0b488542cf0bc9f
push id36593
push userahalberstadt@mozilla.com
push dateMon, 21 Nov 2016 20:06:39 +0000
reviewerssmacleod
bugs1318488
milestone53.0a1
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
python/mozlint/mozlint/cli.py
--- 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))