Handle sys.__stderr__.encoding not existing (#4599)

Handle sys.__stderr__.encoding not existing
This commit is contained in:
Paul Moore 2017-07-05 15:46:36 +01:00 committed by GitHub
parent ed60cdf10c
commit d3f040e120
2 changed files with 9 additions and 1 deletions

1
news/3356.bugfix Normal file
View File

@ -0,0 +1 @@
Don't assume sys.__stderr__.encoding exists

View File

@ -96,7 +96,14 @@ def console_to_str(data):
# redirected and if we don't find an encoding we skip this
# step (on the assumption that output is wrapped by something
# that won't fail).
output_encoding = sys.__stderr__.encoding
# The double getattr is to deal with the possibility that we're
# being called in a situation where sys.__stderr__ doesn't exist,
# or doesn't have an encoding attribute. Neither of these cases
# should occur in normal pip use, but there's no harm in checking
# in case people use pip in (unsupported) unusual situations.
output_encoding = getattr(getattr(sys, "__stderr__", None),
"encoding", None)
if output_encoding:
s = s.encode(output_encoding, errors="backslashreplace")
s = s.decode(output_encoding)