Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'debugging': 0.05; 'try:': 0.07; 'exit.': 0.09; 'frameworks,': 0.09; 'mode,': 0.09; 'snippet': 0.09; 'url:activestate': 0.09; 'cc:addr:python-list': 0.10; 'anyway': 0.11; 'gui': 0.11; 'accidental': 0.16; 'blocks': 0.16; 'conditional': 0.16; 'oct': 0.16; 'poll': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'true:': 0.16; 'try/except': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'thanks,': 0.18; 'trying': 0.21; 'keyboard': 0.22; 'of.': 0.22; 'cc:2**0': 0.23; 'ignored.': 0.23; "python's": 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; '(which': 0.26; 'execution': 0.27; 'message- id:@mail.gmail.com': 0.27; '+0100,': 0.29; 'block,': 0.29; "d'aprano": 0.29; 'loop,': 0.29; 'steven': 0.29; 'url:mailman': 0.29; 'url:code': 0.29; 'probably': 0.29; 'received:209.85.210.174': 0.30; 'code': 0.31; 'point': 0.31; '(and': 0.32; 'url:python': 0.32; 'url:listinfo': 0.32; 'likely': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'needed': 0.35; 'fail': 0.35; 'subject:?': 0.35; 'continue': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'next': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'url:org': 0.36; 'depends': 0.36; 'subject:with': 0.36; 'why': 0.37; 'previous': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'back': 0.62; 'here': 0.65; 'treat': 0.65; 'believe': 0.69; 'press': 0.71; 'receive': 0.71; 'directly.': 0.78; 'andrea': 0.84; 'transactions': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=daFyB+EbES8OSqxB2lyborgP8UX46jUG4Y1VVsBfoyo=; b=UjwInUPNrEA+WMfUFLYd4FwAmJ8VabNn/q3rnARdYzUf1z2efpFtoRmZ4MYkc8/FYE 1L55VXNlXj0IfvK1V7czJnn7fnhv5TtyAQOCgn7Igv/i54Zj2fN5ZLOKWn1Pveaq1Z9r wiiWY7HtSbcXVmNQnVwubPggDMqPLek4iGkAtGrPR8HGi0u7OZuknoFtykJKWowVv42V VExHYtZU/BfbK7+gYGTWQHibjuJcIvukdOhjvhbI5fNExaWpzdHc0yAJxZXKyZdbebRD kn3ocAq3Jy2vsHUGAcceTNP03i+yyD7Stdbm9rH/3RKSj5Bab1xXp47CD4IQnNKb7kTN 3y+w== MIME-Version: 1.0 In-Reply-To: <5088929e$0$29978$c3e8da3$5496439d@news.astraweb.com> References: <5088929e$0$29978$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 25 Oct 2012 15:27:24 +0100 Subject: Re: resume execution after catching with an excepthook? From: andrea crotti To: "Steven D'Aprano" Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351175253 news.xs4all.nl 6849 [2001:888:2000:d::a6]:39064 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32129 2012/10/25 Steven D'Aprano : > 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..