简体   繁体   English

尝试环绕的导入并捕获KeyboardInterrupt

[英]Try-surrounded imports and catching KeyboardInterrupt

I'm seeing a lot of the following pattern in a codebase I'm checking out at the moment: 我现在正在检查的代码库中看到许多以下模式:

try:
    import moduleA
    import moduleB
    from custom.module.A import AX
    from custom.module.A import AY
except KeyboardInterrupt:
    sys.exit()

Haven't seen it before. 没看过。 What's this guarding against? 这要防什么呢?

The only way I can think of that makes sense is if some of those modules for some reason have input / raw_input that run inside of them, or otherwise deliberately raise KeyboardInterrupt for some reason. 我认为这是有意义的唯一方法是,如果其中某些modules出于某种原因在其中运行了input / raw_input ,或者出于某种原因故意raise KeyboardInterrupt

Otherwise, really not quite sure what it's meant to do... (unless some of the imports take hours to run, and if you get fed up, can abandon it without seeing a traceback - but that doesn't make much sense either) 否则,真的不太确定要做什么……(除非某些导入需要花费数小时才能运行,并且如果您受够了,可以放弃它而不会看到回溯,但这也没有多大意义)

Whenever you press ctrl + C from your keyboard, a KeyboardInterrupt is sent to the python process. 每当您从键盘按Ctrl + C时 ,KeyboardInterrupt就会发送到python进程。 If not caught, it will cause an exception in the code so that the code exits wherever it is currently. 如果没有被捕获,它将在代码中引起异常,从而使代码退出当前所在的任何位置。 In this case, there is no special action being taken, but, just a call to sys.exit() , which again causes the program to exit, but, without displaying the stack traceback 在这种情况下,没有采取任何特殊的措施,只是对sys.exit()的调用,这再次导致程序退出,但是没有显示堆栈回溯

From the documentation: 从文档中:

http://docs.python.org/2/library/exceptions.html?highlight=keyboardinterrupt#exceptions.KeyboardInterrupt http://docs.python.org/2/library/exceptions.html?highlight=keyboardinterrupt#exceptions.KeyboardInterrupt

It's not guarding against anything, at least not obviously. 它并没有防范任何事情,至少显然没有。 It's catching a KeyboardInterrupt : 它正在捕获KeyboardInterrupt

Raised when the user hits the interrupt key (normally Control-C or Delete). 当用户按下中断键(通常为Control-C或Delete)时触发。 During execution, a check for interrupts is made regularly. 在执行期间,会定期检查中断。 The exception inherits from BaseException so as to not be accidentally caught by code that catches Exception and thus prevent the interpreter from exiting. 异常继承自BaseException,以便不会被捕获Exception的代码意外捕获,从而防止解释器退出。

Then simply exiting, which is what an interrupt would do anyway. 然后简单地退出,这就是中断将要执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试/除了没有在KeyboardInterrupt上捕获UnboundLocalError - Try/Except not catching UnboundLocalError on KeyboardInterrupt 在KeyboardInterrupt()上捕获回溯 - Catching the Traceback on KeyboardInterrupt() 避免在Python 2.4中意外捕获KeyboardInterrupt和SystemExit - Avoiding accidentally catching KeyboardInterrupt and SystemExit in Python 2.4 Python 2.7:子线程无法捕获KeyboardInterrupt - Python 2.7: Child thread not catching KeyboardInterrupt 是否有“ try:input()除了KeyboardInterrupt:”之外的解决方法 - Is there a workaround for “try: input() except KeyboardInterrupt:” 在程序关闭期间在 Python 中捕获 KeyboardInterrupt - Catching KeyboardInterrupt in Python during program shutdown Python 在使用代码运行器停止时未捕获键盘中断 - Python not catching KeyboardInterrupt when stopping with code runner 在没有 try-except 的情况下在 Python 中捕获键盘中断 - Capture keyboardinterrupt in Python without try-except 如何实现try / except的结果与键盘中断相同? - How to implement a try/except with the same result as a keyboardinterrupt? 停止单击(python 模块)从捕获/处理 KeyboardInterrupt - Stop click (python module) from catching/handling KeyboardInterrupt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM