From c5233c0c18211fd9c8c90de202b124e6fcb96e67 Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Thu, 7 Nov 2024 21:06:56 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Handle=20`KeyboardInterrupt`=20sepa?= =?UTF-8?q?rately=20from=20other=20exceptions=20(#1039)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- tests/test_exit_errors.py | 13 +++++++++++++ typer/core.py | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test_exit_errors.py b/tests/test_exit_errors.py index 598308b1d1..1fdc3e36b0 100644 --- a/tests/test_exit_errors.py +++ b/tests/test_exit_errors.py @@ -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() diff --git a/typer/core.py b/typer/core.py index 84a151a0f1..d9d651aaf8 100644 --- a/typer/core.py +++ b/typer/core.py @@ -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