From d3f040e1201f36bcbdcb8e41f341180a677fd772 Mon Sep 17 00:00:00 2001 From: Paul Moore Date: Wed, 5 Jul 2017 15:46:36 +0100 Subject: [PATCH] Handle sys.__stderr__.encoding not existing (#4599) Handle sys.__stderr__.encoding not existing --- news/3356.bugfix | 1 + pip/compat.py | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 news/3356.bugfix diff --git a/news/3356.bugfix b/news/3356.bugfix new file mode 100644 index 000000000..71aa80617 --- /dev/null +++ b/news/3356.bugfix @@ -0,0 +1 @@ +Don't assume sys.__stderr__.encoding exists diff --git a/pip/compat.py b/pip/compat.py index 8018b2c8f..08cf2a14f 100644 --- a/pip/compat.py +++ b/pip/compat.py @@ -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)