Skip to content

Commit

Permalink
✨ Handle KeyboardInterrupt separately from other exceptions (#1039)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
patrick91 and pre-commit-ci[bot] authored Nov 7, 2024
1 parent e31fc57 commit c5233c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions tests/test_exit_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ def main():
assert result.exit_code == 1


def test_keyboardinterrupt():
# Mainly for coverage/completeness
app = typer.Typer()

@app.command()
def main():
raise KeyboardInterrupt()

result = runner.invoke(app)
assert result.exit_code == 130
assert result.stdout == ""


def test_oserror():
# Mainly for coverage/completeness
app = typer.Typer()
Expand Down
4 changes: 3 additions & 1 deletion typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ def _main(
# even always obvious that `rv` indicates success/failure
# by its truthiness/falsiness
ctx.exit()
except (EOFError, KeyboardInterrupt) as e:
except EOFError as e:
click.echo(file=sys.stderr)
raise click.Abort() from e
except KeyboardInterrupt as e:
raise click.exceptions.Exit(130) from e
except click.ClickException as e:
if not standalone_mode:
raise
Expand Down

0 comments on commit c5233c0

Please sign in to comment.