|
1 | 1 | from __future__ import absolute_import
|
2 | 2 |
|
3 | 3 | import contextlib
|
| 4 | +import errno |
4 | 5 | import logging
|
5 | 6 | import logging.handlers
|
6 | 7 | import os
|
| 8 | +import sys |
| 9 | + |
| 10 | +from pip._vendor.six import PY2 |
7 | 11 |
|
8 | 12 | from pip._internal.utils.compat import WINDOWS
|
9 | 13 | from pip._internal.utils.misc import ensure_dir
|
@@ -33,6 +37,23 @@ class BrokenStdoutLoggingError(Exception):
|
33 | 37 | pass
|
34 | 38 |
|
35 | 39 |
|
| 40 | +if PY2: |
| 41 | + # BrokenPipeError does not exist in Python 2. |
| 42 | + def _is_broken_pipe_error(exc_class, exc): |
| 43 | + """ |
| 44 | + Return whether an exception is a broken pipe error. |
| 45 | +
|
| 46 | + Args: |
| 47 | + exc_class: an exception class. |
| 48 | + exc: an exception instance. |
| 49 | + """ |
| 50 | + return (exc_class is IOError and exc.errno == errno.EPIPE) |
| 51 | + |
| 52 | +else: |
| 53 | + def _is_broken_pipe_error(exc_class, exc): |
| 54 | + return (exc_class is BrokenPipeError) # noqa: F821 |
| 55 | + |
| 56 | + |
36 | 57 | @contextlib.contextmanager
|
37 | 58 | def indent_log(num=2):
|
38 | 59 | """
|
@@ -103,6 +124,16 @@ def __init__(self, stream=None, no_color=None):
|
103 | 124 | if WINDOWS and colorama:
|
104 | 125 | self.stream = colorama.AnsiToWin32(self.stream)
|
105 | 126 |
|
| 127 | + def _using_stdout(self): |
| 128 | + """ |
| 129 | + Return whether the handler is using sys.stdout. |
| 130 | + """ |
| 131 | + if WINDOWS and colorama: |
| 132 | + # Then self.stream is an AnsiToWin32 object. |
| 133 | + return self.stream.wrapped is sys.stdout |
| 134 | + |
| 135 | + return self.stream is sys.stdout |
| 136 | + |
106 | 137 | def should_color(self):
|
107 | 138 | # Don't colorize things if we do not have colorama or if told not to
|
108 | 139 | if not colorama or self._no_color:
|
@@ -135,6 +166,19 @@ def format(self, record):
|
135 | 166 |
|
136 | 167 | return msg
|
137 | 168 |
|
| 169 | + # The logging module says handleError() can be customized. |
| 170 | + def handleError(self, record): |
| 171 | + exc_class, exc = sys.exc_info()[:2] |
| 172 | + # If a broken pipe occurred while calling write() or flush() on the |
| 173 | + # stdout stream in logging's Handler.emit(), then raise our special |
| 174 | + # exception so we can handle it in main() instead of logging the |
| 175 | + # broken pipe error and continuing. |
| 176 | + if (exc_class and self._using_stdout() and |
| 177 | + _is_broken_pipe_error(exc_class, exc)): |
| 178 | + raise BrokenStdoutLoggingError() |
| 179 | + |
| 180 | + return super(ColorizedStreamHandler, self).handleError(record) |
| 181 | + |
138 | 182 |
|
139 | 183 | class BetterRotatingFileHandler(logging.handlers.RotatingFileHandler):
|
140 | 184 |
|
|
0 commit comments