Use --encoding when outputting
This commit is contained in:
parent
9afb76c5ad
commit
734f90bb41
2 changed files with 29 additions and 16 deletions
|
@ -910,22 +910,25 @@ def platform_name():
|
|||
return res
|
||||
|
||||
|
||||
def write_string(s, out=None):
|
||||
def write_string(s, out=None, encoding=None):
|
||||
if out is None:
|
||||
out = sys.stderr
|
||||
assert type(s) == compat_str
|
||||
|
||||
if ('b' in getattr(out, 'mode', '') or
|
||||
sys.version_info[0] < 3): # Python 2 lies about mode of sys.stderr
|
||||
s = s.encode(preferredencoding(), 'ignore')
|
||||
s = s.encode(encoding or preferredencoding(), 'ignore')
|
||||
try:
|
||||
out.write(s)
|
||||
except UnicodeEncodeError:
|
||||
# In Windows shells, this can fail even when the codec is just charmap!?
|
||||
# See https://wiki.python.org/moin/PrintFails#Issue
|
||||
if sys.platform == 'win32' and hasattr(out, 'encoding'):
|
||||
s = s.encode(out.encoding, 'ignore').decode(out.encoding)
|
||||
out.write(s)
|
||||
if sys.platform == 'win32':
|
||||
if not encoding and hasattr(out, 'encoding'):
|
||||
encoding = out.encoding
|
||||
if encoding:
|
||||
b = s.encode(encoding, 'ignore').decode(encoding)
|
||||
out.write(b)
|
||||
else:
|
||||
raise
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue