Reputation: 5648
I'am writing console program. I want that save flow may break with Cntr-c only after answer on question: Do you really want break it?
def sigint_handler(signal, frame):
try:
Exit = (str(raw_input("Break ? Y/N")))
if Exit == "Y" or Exit=="y":
raise KeyboardInterrupt()
except KeyboardInterrupt:
raise KeyboardInterrupt()
except Exception as e:
pass
signal.signal(signal.SIGINT,sigint_handler)
i=0
while i<1000:
i=i+1
print "%d\n"%i
sleep(0.5)
It's fail if I try cntl+c instead of Y:
71
72
73
74
75
^CBreak ? Y/Ny
File "/home.local/valerys/rde_1_3/rdepyui/bin/../api/cli.py", line 48, in sigint_handler Exit = (str(raw_input("Break ? Y/N"))) RuntimeError: can't re-enter readline
Upvotes: 0
Views: 2868
Reputation: 2786
Why do you make a re-raise of KeyboardInterrupt
in the except
block? In this way you catch the first KeyboardInterrupt
but you don't have another try/except block for catching the second. Maybe a better solution is to call
try:
Exit = (str(raw_input("Break ? Y/N")))
if Exit == "Y" or Exit=="y":
raise KeyboardInterrupt()
except KeyboardInterrupt:
sys.exit()
for a clean exit strategy. I hope this can help you.
Upvotes: 3