Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19659

RE: contextlib.contextmanager and try/finally

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'mentioned,': 0.04; 'sys': 0.05; 'fashion.': 0.07; 'finally:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'exception': 0.12; 'def': 0.13; '"i\'d': 0.16; 'bye': 0.16; 'closes': 0.16; 'contextlib': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.16; '(i.e.': 0.17; '>>>': 0.18; 'written': 0.19; 'mechanism': 0.21; 'from:addr:web.de': 0.23; 'timely': 0.25; 'import': 0.27; 'explicitly': 0.28; 'yield': 0.28; 'print': 0.29; 'terminate': 0.30; 'yields': 0.30; "won't": 0.33; 'to:addr:python-list': 0.33; 'rather': 0.34; 'subject:/': 0.34; 'skip:@ 10': 0.34; 'try:': 0.34; 'header:X-Complaints-To:1': 0.34; 'something': 0.35; 'uses': 0.36; 'thread': 0.36; 'but': 0.37; 'received:org': 0.37; 'another': 0.37; 'happens': 0.37; "i'd": 0.39; 'except': 0.39; 'called': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'your': 0.61; 'program:': 0.67; 'guaranteed': 0.77; 'subject:try': 0.84; 'sys.exit()?': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject RE: contextlib.contextmanager and try/finally
Date Tue, 31 Jan 2012 23:02:22 +0100
Organization None
References <10841855.1646.1326296715536.JavaMail.geo-discussion-forums@yqiz15> <CALwzidntt7n1PskCuoHaehCR1+K_F13YxdGapW2fWQtykNYm+A@mail.gmail.com> <5B80DD153D7D744689F57F4FB69AF474103948@SCACMX008.exchad.jpmchase.net>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084ac3a.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5260.1328047355.27778.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328047355 news.xs4all.nl 6849 [2001:888:2000:d::a6]:51223
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19659

Show key headers only | View raw


Prasad, Ramit wrote:

>>Like Neil mentioned, a contextmanager generator is wrapped with an
>>__exit__ method that is guaranteed to be called and that explicitly
>>resumes or closes the generator.  So as long as your contextmanager
>>generator is properly written (i.e. it yields exactly once), the
>>finally block will execute in a timely fashion.
> 
> Is that true even in the face of something like sys.exit()?
> What happens if 1) sys.exit is called while in the same thread
> 2) sys.exit is called from another thread but while this thread
> is in context manager?

sys.exit() uses the normal exception mechanism to terminate a program:

>>> import sys
>>> try:
...     sys.exit()
... except SystemExit:
...     print "I'd rather not"
...
I'd rather not
>>>

It won't derail a context manager:

>>> from contextlib import contextmanager
>>> @contextmanager
... def f():
...     try:
...             yield
...     finally: print "bye"
...
>>> import sys
>>> with f():
...     sys.exit()
...
bye
$

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

contextlib.contextmanager and try/finally johannh@gmail.com - 2012-01-11 07:45 -0800
  Re: contextlib.contextmanager and try/finally Neil Cerutti <neilc@norwich.edu> - 2012-01-11 16:46 +0000
  Re: contextlib.contextmanager and try/finally Robert Kern <robert.kern@gmail.com> - 2012-01-11 17:14 +0000
  Re: contextlib.contextmanager and try/finally Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-11 10:20 -0700
    Re: contextlib.contextmanager and try/finally johannh@gmail.com - 2012-01-11 09:30 -0800
    Re: contextlib.contextmanager and try/finally johannh@gmail.com - 2012-01-11 09:30 -0800
  RE: contextlib.contextmanager and try/finally "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-01-31 21:07 +0000
  RE: contextlib.contextmanager and try/finally Peter Otten <__peter__@web.de> - 2012-01-31 23:02 +0100
  Re: contextlib.contextmanager and try/finally Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-31 15:09 -0700
    Re: contextlib.contextmanager and try/finally Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-01 02:15 +0000

csiph-web