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

✨ Handle KeyboardInterrupt separately from other exceptions #1039

Merged
merged 3 commits into from
Nov 7, 2024
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
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
Loading