Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #94514
| Path | csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.010 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'context': 0.05; '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; 'def': 0.13; 'great!': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:u 50': 0.16; 'stack:': 0.16; 'wrote:': 0.16; 'tried': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'package.': 0.27; 'looks': 0.29; "i'm": 0.30; 'code': 0.30; 'skip:s 30': 0.31; 'url:python': 0.33; 'this?': 0.34; 'skip:d 20': 0.34; 'so,': 0.35; 'something': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; 'received:org': 0.37; 'missing': 0.37; 'sure': 0.39; 'to:addr:python.org': 0.40; 'url:3': 0.60; 'sounds': 0.76; 'actually,': 0.84; 'becker': 0.84; 'received:139': 0.91; 'try.': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Neal Becker <ndbecker2@gmail.com> |
| Subject | Re: register cleanup handler |
| Date | Fri, 24 Jul 2015 13:35:12 -0400 |
| References | <mailman.950.1437749869.3674.python-list@python.org> <55b26ad1$0$2920$e4fe514c@news.xs4all.nl> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.954.1437759337.3674.python-list@python.org> (permalink) |
| Lines | 47 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1437759337 news.xs4all.nl 2841 [2001:888:2000:d::a6]:48884 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:94514 |
Show key headers only | View raw
Irmen de Jong wrote:
> On 24-7-2015 16:57, Neal Becker wrote:
>> I have code like:
>>
>> if (condition):
>> do_something_needing_cleanup
>>
>> code_executed_unconditionally
>>
>> <cleanup has to happen here if required, even if above did return,
>> continue or exception>
>>
>> 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?
>>
>
> Sounds like you want a context manager, see
> https://docs.python.org/3/library/contextlib.html#replacing-any-use-of-try-finally-and-flag-variables
>
> Irmen
Yes, that looks great! I'm using py2.7.10, so I tried 'contexter' package.
if (condition):
with ExitStack() as stack:
if condition:
do_something()
def cleanup_resources():
some_cleanup_using_closure()
stack.callback(cleanup_resources)
unconditionally_executed_code_no_worrying_about_cleanup()
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
register cleanup handler Neal Becker <ndbecker2@gmail.com> - 2015-07-24 10:57 -0400
Re: register cleanup handler Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2015-07-24 18:41 +0200
Re: register cleanup handler Neal Becker <ndbecker2@gmail.com> - 2015-07-24 13:35 -0400
csiph-web