Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'main()': 0.05; 'received:verizon.net': 0.07; 'terry': 0.07; '__name__': 0.09; 'exceptions': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'am,': 0.12; 'def': 0.13; "'__main__':": 0.16; '*always*': 0.16; 'atexit': 0.16; 'main():': 0.16; 'quits': 0.16; 'reedy': 0.16; 'simplified': 0.16; 'subject:case': 0.16; 'wrote:': 0.18; 'jan': 0.19; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'function': 0.27; 'import': 0.27; 'exit': 0.29; 'subject:skip:a 10': 0.29; 'problem': 0.29; 'returns,': 0.30; 'there': 0.33; 'header:User- Agent:1': 0.33; 'file': 0.34; 'header:X-Complaints-To:1': 0.34; 'rather': 0.34; 'try:': 0.34; 'to:addr:python-list': 0.35; 'received:org': 0.36; 'created': 0.37; 'but': 0.37; 'some': 0.38; 'except': 0.39; 'application': 0.40; 'to:addr:python.org': 0.40; 'andrea': 0.84; 'execution.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: atexit.register in case of errors Date: Wed, 15 Feb 2012 16:31:19 -0500 References: <4F3BAF26.2060505@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <4F3BAF26.2060505@gmail.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1329341500 news.xs4all.nl 6966 [2001:888:2000:d::a6]:57787 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20478 On 2/15/2012 8:12 AM, Andrea Crotti wrote: > I have the following very simplified situation > > from atexit import register > > def goodbye(): print("saying goodbye") > > def main(): > while True: var = raw_input("read something") > > if __name__ == '__main__': > register(goodbye) > main() > > But in my case the "goodbye" function is deleting the logging file > which was created during the application execution. Now the problem > is that it *always* executes, even when the applications quits for > some bad errors. > > Is there a way to have an exit hook, which doesn't execute in case of > errors? Have a single no-error normal exit point. if __name__ == '__main__': main() cleanup() if you really want to exit by exceptions rather than by returns, if __name__ == '__main__': try: main() except SystemExit: normal_exit_cleanup() -- Terry Jan Reedy