def _get_text_writer(stream, errors):
# In python3, all the sys.stdout/sys.stderr streams are in text
# mode. This means they expect unicode, and will encode the
# unicode automatically before actually writing to stdout/stderr.
# In python2, that's not the case. In order to provide a consistent
# interface, we can create a wrapper around sys.stdout that will take
# unicode, and automatically encode it to the preferred encoding.
# That way consumers can just call get_text_writer(stream) and write
# unicode to the returned stream. Note that get_text_writer
# just returns the stream in the PY3 section above because python3
# handles this.
# We're going to use the preferred encoding, but in cases that there is
# no preferred encoding we're going to fall back to assuming ASCII is
# what we should use. This will currently break the use of
# PYTHONIOENCODING, which would require checking stream.encoding first,
# however, the existing behavior is to only use
# locale.getpreferredencoding() and so in the hope of not breaking what
# is currently working, we will continue to only use that.
encoding = locale.getpreferredencoding()
if encoding is None:
encoding = "ascii"
return codecs.getwriter(encoding)(stream, errors)
评论列表
文章目录