resume execution after catching with an excepthook?

Chris Angelico rosuav at gmail.com
Thu Oct 25 12:02:11 EDT 2012


On Fri, Oct 26, 2012 at 2:31 AM, Hans Mulder <hansmu at xs4all.nl> wrote:
> 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)
>

Yes, that's what I was talking about. You do have to be fairly careful
what you do (for instance, what should happen if the user hits Ctrl-C
during handler()? Default is that it'll raise KeyboardInterrupt
unconditionally), but you have perfect flexibility.

ChrisA



More information about the Python-list mailing list