Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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.028 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'cleanup': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'code': 0.30; 'this?': 0.34; 'skip:d 20': 0.34; 'so,': 0.35; 'something': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'really': 0.37; 'being': 0.37; 'received:org': 0.37; 'missing': 0.37; 'sure': 0.39; 'to:addr:python.org': 0.40; 'actually,': 0.84; 'received:139': 0.91; 'try.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Neal Becker Subject: register cleanup handler Date: Fri, 24 Jul 2015 10:57:30 -0400 Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: exa2-in-fw-01-epn.hns.com User-Agent: KNode/4.14.9 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437749869 news.xs4all.nl 2821 [2001:888:2000:d::a6]:43219 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94507 I have code like: if (condition): do_something_needing_cleanup code_executed_unconditionally Now, how can I make sure cleanup happens? Actually, what I really would like, is: if (condition): do_something_needing_cleanup register_scoped_cleanup (cleanup_fnc) code_executed_unconditionally So, any thoughts/hopes of python being able to do something like this? I know we have try/finally, but I don't think that helps here, because code_executed_unconditionally couldn't be inside the try. Or am I missing something obvious?