On 24/10/12 14:51:30, andrea crotti wrote: > So I would like to be able to ask for confirmation when I receive a C-c, > and continue if the answer is "N/n". > > I'm already using an exception handler set with sys.excepthook, but I > can't make it work with the confirm_exit, because it's going to quit in > any case.. > > A possible solution would be to do a global "try/except > KeyboardInterrupt", but since I already have an excepthook I wanted to > use this. Any way to make it continue where it was running after the > exception is handled? > > > def confirm_exit(): > while True: > q = raw_input("This will quit the program, are you sure? [y/N]") > if q in ('y', 'Y'): > sys.exit(0) > elif q in ('n', 'N'): > print("Continuing execution") > # just go back to normal execution, is it possible?? > break > > > def _exception_handler(etype, value, tb): > if etype == KeyboardInterrupt: > confirm_exit() > else: > sys.exit(1) > > > def set_exception_handler(): > sys.excepthook = _exception_handler
I think the trick is to not use an except hook, but trap the interrupt on a lower level. This seems to work; I'm not sure how robust it is: import signal def handler(signum, frame): while True: q = raw_input("This will quit the program, are you sure? [y/N]") if q[:1] in "yY": raise KeyboardInterrupt elif q[:1] in "nN": print("Continuing execution") # just go back to normal execution return signal.signal(signal.SIGINT, handler) If you're debugging this on a Unix platform, it may help to know that you can also kill a process with control-\ Hope this helps, -- HansM -- http://mail.python.org/mailman/listinfo/python-list