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

Custom abort behaviour #2112

Closed
alexreg opened this issue Oct 28, 2021 · 6 comments
Closed

Custom abort behaviour #2112

alexreg opened this issue Oct 28, 2021 · 6 comments

Comments

@alexreg
Copy link
Contributor

alexreg commented Oct 28, 2021

Currently, when Ctrl+C is pressed, a Click program "aborts" (printing "Aborted!") and immediately exits. I would like to be able to customise this behaviour, principally for the purpose of executing "teardown" code before the app exits, but it might also be nice to be able to customise the message to conform with a particular message style.

As far as I'm aware, there is no other way to solve this without turning off exception handling entirely, which is far from ideal.

@roblight
Copy link

roblight commented Dec 2, 2021

This really caught me for a surprise. I'm using Typer for a CLI app and "Aborted!" message showed up on keyboard interrupt. No easy way to change or silence it appears.

@alexreg
Copy link
Contributor Author

alexreg commented Dec 2, 2021

@roblight Yeah, it's confusing. I originally thought the program had crashed with some memory issue (SIGABRT). I ended up doing exception handling manually in my case, in order to override this.

@davidism
Copy link
Member

davidism commented Dec 2, 2021

It sounds like what you both are asking for is just to not print the "Aborted" message, not to be able to customize it. As to teardown, you should use the standard atexit module to accomplish that, it's not specific to Click.

@alexreg
Copy link
Contributor Author

alexreg commented Dec 3, 2021

@davidism I'm not sure if existing signal handlers can be removed using atexit, but that seems a bit hacky in any case.

@davidism
Copy link
Member

davidism commented Dec 28, 2021

Using the atexit module to do something at exit is not a hack, it's the intended use case of a standard module.

Perhaps you're looking for context managers? https://click.palletsprojects.com/en/8.0.x/advanced/#managing-resources

@alexreg
Copy link
Contributor Author

alexreg commented Dec 28, 2021

Would I be able to remove the default exit handle in that case? The one that click provides.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants