Skip to content

Commit

Permalink
Do not suppress EOFError/KeyboardInterrupt when standalone is disabled
Browse files Browse the repository at this point in the history
Raise Abort() exception in a chain with the original exception.
  • Loading branch information
atugushev authored and davidism committed Jun 30, 2023
1 parent df9ad40 commit 422e211
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Unreleased
- Improve command name detection when using Shiv or PEX. :issue:`2332`
- Avoid showing empty lines if command help text is empty. :issue:`2368`
- ZSH completion script works when loaded from ``fpath``. :issue:`2344`.
- ``EOFError`` and ``KeyboardInterrupt`` tracebacks are not suppressed when
``standalone_mode`` is disabled. :issue:`2380`


Version 8.1.3
Expand Down
4 changes: 2 additions & 2 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,9 +1086,9 @@ def main(
# even always obvious that `rv` indicates success/failure
# by its truthiness/falsiness
ctx.exit()
except (EOFError, KeyboardInterrupt):
except (EOFError, KeyboardInterrupt) as e:
echo(file=sys.stderr)
raise Abort() from None
raise Abort() from e
except ClickException as e:
if not standalone_mode:
raise
Expand Down
12 changes: 12 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,15 @@ def command2(e):

runner.invoke(group, ["command1"])
assert opt_prefixes == {"-", "--", "~", "+"}


@pytest.mark.parametrize("exc", (EOFError, KeyboardInterrupt))
def test_abort_exceptions_with_disabled_standalone_mode(runner, exc):
@click.command()
def cli():
raise exc("catch me!")

rv = runner.invoke(cli, standalone_mode=False)
assert rv.exit_code == 1
assert isinstance(rv.exception.__cause__, exc)
assert rv.exception.__cause__.args == ("catch me!",)

0 comments on commit 422e211

Please sign in to comment.