Commit 0e3efec2336062ee75d574192d4f06a9a3ae0961

Authored by Christian Herdtweck
1 parent ae5ff5e4

io_encoding: warn when modifying encoding

oletools/common/io_encoding.py
@@ -128,8 +128,8 @@ def ensure_stdout_handles_unicode(): @@ -128,8 +128,8 @@ def ensure_stdout_handles_unicode():
128 elif os.isatty(output_stream.fileno()): # e.g. C locale 128 elif os.isatty(output_stream.fileno()): # e.g. C locale
129 # Do not output UTF8 since that might be mis-interpreted. 129 # Do not output UTF8 since that might be mis-interpreted.
130 # Just replace chars that cannot be handled 130 # Just replace chars that cannot be handled
131 - if DEBUG:  
132 - print('sys.stdout is a tty, just replace errors') 131 + print('Encoding for stdout is only {}, will replace other chars to '
  132 + 'avoid unicode error'.format(encoding), file=sys.stderr)
133 sys.stdout = codecs.getwriter(encoding)(output_stream, errors='replace') 133 sys.stdout = codecs.getwriter(encoding)(output_stream, errors='replace')
134 else: # e.g. redirection, pipe in python2 134 else: # e.g. redirection, pipe in python2
135 new_encoding = PREFERRED_ENCODING 135 new_encoding = PREFERRED_ENCODING
@@ -141,6 +141,8 @@ def ensure_stdout_handles_unicode(): @@ -141,6 +141,8 @@ def ensure_stdout_handles_unicode():
141 if DEBUG: 141 if DEBUG:
142 print('preferred encoding also unacceptable, fall back to {}' 142 print('preferred encoding also unacceptable, fall back to {}'
143 .format(new_encoding)) 143 .format(new_encoding))
  144 + print('Encoding for stdout is only {}, will auto-encode text with {} '
  145 + 'before output'.format(encoding, new_encoding), file=sys.stderr)
144 sys.stdout = codecs.getwriter(new_encoding)(output_stream) 146 sys.stdout = codecs.getwriter(new_encoding)(output_stream)
145 147
146 148