diff options
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/misc.py | 14 | ||||
-rw-r--r-- | coverage/python.py | 1 | ||||
-rw-r--r-- | coverage/summary.py | 5 |
3 files changed, 16 insertions, 4 deletions
diff --git a/coverage/misc.py b/coverage/misc.py index 36e4fe9c..db6298b6 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -6,7 +6,9 @@ import errno import hashlib import inspect +import locale import os +import sys import types from coverage import env @@ -149,6 +151,18 @@ def file_be_gone(path): raise +def output_encoding(outfile=None): + """Determine the encoding to use for output written to `outfile` or stdout.""" + if outfile is None: + outfile = sys.stdout + encoding = ( + getattr(outfile, "encoding", None) or + getattr(sys.__stdout__, "encoding", None) or + locale.getpreferredencoding() + ) + return encoding + + class Hasher(object): """Hashes Python data into md5.""" def __init__(self): diff --git a/coverage/python.py b/coverage/python.py index a4247ce6..4f589735 100644 --- a/coverage/python.py +++ b/coverage/python.py @@ -4,7 +4,6 @@ """Python source expertise for coverage.py""" import os.path -import sys import zipimport from coverage import env, files diff --git a/coverage/summary.py b/coverage/summary.py index f797e306..5ddbb380 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -8,7 +8,7 @@ import sys from coverage import env from coverage.report import Reporter from coverage.results import Numbers -from coverage.misc import NotPython, CoverageException +from coverage.misc import NotPython, CoverageException, output_encoding class SummaryReporter(Reporter): @@ -52,8 +52,7 @@ class SummaryReporter(Reporter): outfile = sys.stdout if env.PY2: - encoding = getattr(outfile, "encoding", None) or sys.getfilesystemencoding() - writeout = lambda u: outfile.write(u.encode(encoding)) + writeout = lambda u: outfile.write(u.encode(output_encoding())) else: writeout = outfile.write |