Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32129
| References | <mailman.2758.1351083094.27098.python-list@python.org> <5088929e$0$29978$c3e8da3$5496439d@news.astraweb.com> |
|---|---|
| Date | 2012-10-25 15:27 +0100 |
| Subject | Re: resume execution after catching with an excepthook? |
| From | andrea crotti <andrea.crotti.0@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2844.1351175253.27098.python-list@python.org> (permalink) |
2012/10/25 Steven D'Aprano <steve+comp.lang.python@pearwood.info>: > On Wed, 24 Oct 2012 13:51:30 +0100, 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 don't think there is any way to do this directly. > > Without a try...except block, execution will cease after an exception is > caught, even when using sys.excepthook. I don't believe that there is any > way to jump back to the line of code that just failed (and why would you, > it will just fail again) or the next line (which will likely fail because > the previous line failed). > > I think the only way you can do this is to write your own execution loop: > > while True: > try: > run(next_command()) > except KeyboardInterrupt: > if confirm_quit(): > break > > > Of course you need to make run() atomic, or use transactions that can be > reverted or backed out of. How plausible this is depends on what you are > trying to do -- Python's Ctrl-C is not really designed to be ignored. > > Perhaps a better approach would be to treat Ctrl-C as an unconditional > exit, and periodically poll the keyboard for another key press to use as > a conditional exit. Here's a snippet of platform-specific code to get a > key press: > > http://code.activestate.com/recipes/577977 > > Note however that it blocks if there is no key press waiting. > > I suspect that you may need a proper event loop, as provided by GUI > frameworks, or curses. > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list Ok thanks, but here the point is not to resume something that is going to fail again, just to avoid accidental kill of processes that take a long time. Probably needed only by me in debugging mode, but anyway I can do the simple try/except then, thanks..
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
resume execution after catching with an excepthook? andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-24 13:51 +0100
Re: resume execution after catching with an excepthook? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-25 01:15 +0000
Re: resume execution after catching with an excepthook? andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-25 15:27 +0100
Re: resume execution after catching with an excepthook? Chris Angelico <rosuav@gmail.com> - 2012-10-26 01:51 +1100
Re: resume execution after catching with an excepthook? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-25 15:40 +0000
RE: resume execution after catching with an excepthook? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-25 20:59 +0000
Re: resume execution after catching with an excepthook? Hans Mulder <hansmu@xs4all.nl> - 2012-10-25 17:31 +0200
Re: resume execution after catching with an excepthook? Chris Angelico <rosuav@gmail.com> - 2012-10-26 03:02 +1100
csiph-web