Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent error messages/exit code 120 after exiting on EPIPE #1106

Merged
merged 2 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Unreleased
- Add documentation for ``ignore_unkown_options``. (`#684`_)
- Add bright colors support for ``click.style`` and fix the reset option for parameters ``fg`` and ``bg``. (`#703`_, `#809`_)
- Add ``show_envvar`` for showing environment variables in help. (`#710`_)
- Avoid ``BrokenPipeError`` during interpreter shutdown when stdout or stderr is a closed pipe. (`#712`_, `#1106`_)
- Document customizing option names. (`#725`_, `#1016`_)
- Disable ``sys._getframes()`` on Python interpreters that don't support it. (`#728`_)
- Fix bug in test runner when calling ``sys.exit`` with ``None``. (`#739`_)
Expand Down Expand Up @@ -129,6 +130,7 @@ Unreleased
.. _#706: https://github.com/pallets/click/pull/706
.. _#709: https://github.com/pallets/click/pull/709
.. _#710: https://github.com/pallets/click/pull/710
.. _#712: https://github.com/pallets/click/pull/712
.. _#719: https://github.com/pallets/click/issues/719
.. _#725: https://github.com/pallets/click/issues/725
.. _#728: https://github.com/pallets/click/pull/728
Expand Down Expand Up @@ -214,6 +216,7 @@ Unreleased
.. _#1061: https://github.com/pallets/click/pull/1061
.. _#1088: https://github.com/pallets/click/issues/1088
.. _#1105: https://github.com/pallets/click/pull/1105
.. _#1106: https://github.com/pallets/click/pull/1106
.. _#1108: https://github.com/pallets/click/pull/1108
.. _#1115: https://github.com/pallets/click/pull/1115

Expand Down
5 changes: 4 additions & 1 deletion click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from functools import update_wrapper

from .types import convert_type, IntRange, BOOL
from .utils import make_str, make_default_short_help, echo, get_os_args
from .utils import PacifyFlushWrapper, make_str, make_default_short_help, \
echo, get_os_args
from .exceptions import ClickException, UsageError, BadParameter, Abort, \
MissingParameter, Exit
from .termui import prompt, confirm, style
Expand Down Expand Up @@ -734,6 +735,8 @@ def main(self, args=None, prog_name=None, complete_var=None,
sys.exit(e.exit_code)
except IOError as e:
if e.errno == errno.EPIPE:
sys.stdout = PacifyFlushWrapper(sys.stdout)
sys.stderr = PacifyFlushWrapper(sys.stderr)
sys.exit(1)
else:
raise
Expand Down
24 changes: 24 additions & 0 deletions click/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,27 @@ def get_app_dir(app_name, roaming=True, force_posix=False):
return os.path.join(
os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config')),
_posixify(app_name))


class PacifyFlushWrapper(object):
"""This wrapper is used to catch and suppress BrokenPipeErrors resulting
from ``.flush()`` being called on broken pipe during the shutdown/final-GC
of the Python interpreter. Notably ``.flush()`` is always called on
``sys.stdout`` and ``sys.stderr``. So as to have minimal impact on any
other cleanup code, and the case where the underlying file is not a broken
pipe, all calls and attributes are proxied.
"""

def __init__(self, wrapped):
self.wrapped = wrapped

def flush(self):
try:
self.wrapped.flush()
except IOError as e:
import errno
if e.errno != errno.EPIPE:
raise

def __getattr__(self, attr):
return getattr(self.wrapped, attr)