Merge pull request #1106 from frankier/prevent-epipe-exit-120
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Prevent error messages/exit code 120 after exiting on EPIPE
authored by David Lord · committed by GitHub · parents 6e410dc, a257a5d · Browse files · 3 files·+31 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+31 added−1 removed
- other:+31 added−1 removed
Provenance
- Commit
38a2712- Tree
1e90511- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
6e410dc- Parent 2
a257a5d
3 files changed+31 added−1 removed
modified:click/core.py+4 added-1 removed
5 unmodified lines6 from itertools import repeat7 from functools import update_wrapper8 9 from .types import convert_type, IntRange, BOOL10-from .utils import make_str, make_default_short_help, echo, get_os_args10+from .utils import PacifyFlushWrapper, make_str, make_default_short_help, \11+ echo, get_os_args12 from .exceptions import ClickException, UsageError, BadParameter, Abort, \13 MissingParameter, Exit14 from .termui import prompt, confirm, style15 from .formatting import HelpFormatter, join_options718 unmodified lines734 e.show()735 sys.exit(e.exit_code)736 except IOError as e:737 if e.errno == errno.EPIPE:738+ sys.stdout = PacifyFlushWrapper(sys.stdout)739+ sys.stderr = PacifyFlushWrapper(sys.stderr)740 sys.exit(1)741 else:742 raise743 except Exit as e:1113 unmodified linesmodified:click/utils.py+24 added
412 unmodified lines413 '~/Library/Application Support'), app_name)414 return os.path.join(415 os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config')),416 _posixify(app_name))417+418+419+class PacifyFlushWrapper(object):420+ """This wrapper is used to catch and suppress BrokenPipeErrors resulting421+ from ``.flush()`` being called on broken pipe during the shutdown/final-GC422+ of the Python interpreter. Notably ``.flush()`` is always called on423+ ``sys.stdout`` and ``sys.stderr``. So as to have minimal impact on any424+ other cleanup code, and the case where the underlying file is not a broken425+ pipe, all calls and attributes are proxied.426+ """427+428+ def __init__(self, wrapped):429+ self.wrapped = wrapped430+431+ def flush(self):432+ try:433+ self.wrapped.flush()434+ except IOError as e:435+ import errno436+ if e.errno != errno.EPIPE:437+ raise438+439+ def __getattr__(self, attr):440+ return getattr(self.wrapped, attr)modified:CHANGES.rst+3 added
37 unmodified lines38 - Make ``Argument.make_metavar()`` default to type metavar. (`#675`_)39 - Add documentation for ``ignore_unkown_options``. (`#684`_)40 - Add bright colors support for ``click.style`` and fix the reset option for parameters ``fg`` and ``bg``. (`#703`_, `#809`_)41 - Add ``show_envvar`` for showing environment variables in help. (`#710`_)42+- Avoid ``BrokenPipeError`` during interpreter shutdown when stdout or stderr is a closed pipe. (`#712`_, `#1106`_)43 - Document customizing option names. (`#725`_, `#1016`_)44 - Disable ``sys._getframes()`` on Python interpreters that don't support it. (`#728`_)45 - Fix bug in test runner when calling ``sys.exit`` with ``None``. (`#739`_)46 - Clarify documentation on command line options. (`#741`_, `#1003`_)82 unmodified lines129 .. _#704: https://github.com/pallets/click/issues/704130 .. _#706: https://github.com/pallets/click/pull/706131 .. _#709: https://github.com/pallets/click/pull/709132 .. _#710: https://github.com/pallets/click/pull/710133+.. _#712: https://github.com/pallets/click/pull/712134 .. _#719: https://github.com/pallets/click/issues/719135 .. _#725: https://github.com/pallets/click/issues/725136 .. _#728: https://github.com/pallets/click/pull/728137 .. _#739: https://github.com/pallets/click/pull/73977 unmodified lines215 .. _#1059: https://github.com/pallets/click/pull/1059216 .. _#1061: https://github.com/pallets/click/pull/1061217 .. _#1088: https://github.com/pallets/click/issues/1088218 .. _#1105: https://github.com/pallets/click/pull/1105219+.. _#1106: https://github.com/pallets/click/pull/1106220 .. _#1108: https://github.com/pallets/click/pull/1108221 .. _#1115: https://github.com/pallets/click/pull/1115222 223 345 unmodified lines