Skip to content

Commit

Permalink
fix pypa#4170: log ignores broken-pipes in STDERR when pip | head
Browse files Browse the repository at this point in the history
  • Loading branch information
ankostis committed Aug 22, 2018
1 parent 7f4ec24 commit 28beeeb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions news/4170.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Avoid printing a Traceback when a broken pipe is encountered in stderr
(like when piping through `head` on Unix).

10 changes: 10 additions & 0 deletions src/pip/_internal/utils/logging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import

import contextlib
import errno
import logging
import logging.handlers
import os
Expand Down Expand Up @@ -115,6 +116,15 @@ def format(self, record):

return msg

def flush(self):
"""Ignore `BrokenPipeErrors` on `flush()`."""
try:
super(ColorizedStreamHandler, self).flush()
except (IOError, OSError) as ex: # Python-2 has no `BrokenPipeError`
if ex.errno != errno.EPIPE:
# Ignore BrokenPipeError eg. when piped in `head` UNIX command.
raise


class BetterRotatingFileHandler(logging.handlers.RotatingFileHandler):

Expand Down

0 comments on commit 28beeeb

Please sign in to comment.