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


Groups > comp.lang.python > #19661

Re: contextlib.contextmanager and try/finally

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
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; 'context': 0.04; 'mentioned,': 0.04; 'interpreter': 0.05; 'exception,': 0.07; 'fashion.': 0.07; 'method,': 0.07; 'raises': 0.07; 'python': 0.08; 'execute,': 0.09; 'executes': 0.09; 'false,': 0.09; 'interpreter,': 0.09; 'exception': 0.12; 'cc:addr:python-list': 0.15; 'received:74.125.82.44': 0.15; 'received:mail- ww0-f44.google.com': 0.15; '"kill': 0.16; 'closes': 0.16; 'out?': 0.16; 'scenarios': 0.16; 'throws': 0.16; 'wrote:': 0.16; '(i.e.': 0.17; '\xa0so': 0.18; 'written': 0.19; 'jan': 0.19; 'to:2**1': 0.21; 'header:In-Reply-To:1': 0.22; 'clause': 0.23; 'cc:2**0': 0.25; 'timely': 0.25; 'pm,': 0.26; 'message-id:@mail.gmail.com': 0.28; 'explicitly': 0.28; 'cc:addr:python.org': 0.29; 'yes.': 0.30; 'yields': 0.30; 'certainly': 0.32; 'does': 0.32; 'tue,': 0.32; 'question,': 0.34; 'subject:/': 0.34; 'received:74.125.82': 0.34; 'something': 0.35; 'affects': 0.36; 'thread': 0.36; 'but': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'another': 0.37; 'happens': 0.37; 'e.g.': 0.38; 'why': 0.39; 'called': 0.39; 'subject:: ': 0.39; 'your': 0.61; 'power': 0.63; '31,': 0.64; 'guaranteed': 0.77; 'generator,': 0.84; 'subject:try': 0.84; 'sys.exit()?': 0.84; 'killing': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=3nrezUyejmGpxupRhw5hsF/dv6UWnpsK6LAkdTwP5yg=; b=DbRXsCooGQrRrhGxjAloH8S2XJj0Lj/pB9DnMerj4tiJznJSh9HcNAwtFcj5aOtQWJ s1pTKKgGfvf8PLpyCLDD+TudJZNep9tO7QR+jaImpvl6wukTdpmBiNN0yx+NYE6PX21G VjqirXfHcgOCrueGfMbW3rs32sAivozOUyYGk=
MIME-Version 1.0
In-Reply-To <5B80DD153D7D744689F57F4FB69AF474103948@SCACMX008.exchad.jpmchase.net>
References <10841855.1646.1326296715536.JavaMail.geo-discussion-forums@yqiz15> <CALwzidntt7n1PskCuoHaehCR1+K_F13YxdGapW2fWQtykNYm+A@mail.gmail.com> <5B80DD153D7D744689F57F4FB69AF474103948@SCACMX008.exchad.jpmchase.net>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Tue, 31 Jan 2012 15:09:34 -0700
Subject Re: contextlib.contextmanager and try/finally
To "Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc "python-list@python.org" <python-list@python.org>
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.5261.1328047804.27778.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328047804 news.xs4all.nl 6869 [2001:888:2000:d::a6]:46910
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19661

Show key headers only | View raw


On Tue, Jan 31, 2012 at 2:07 PM, Prasad, Ramit
<ramit.prasad@jpmorgan.com> 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()?

Yes.

> What happens if 1) sys.exit is called while in the same thread

Why don't you try it and find out?  To answer the question, though,
sys.exit() raises a SystemExit exception, which propagates out of the
with block and calls the __exit__ method, which then throws the
exception to the generator, which executes its finally clause and
exits.  The __exit__ method returns false, so the SystemExit exception
continues to propagate, and if it is not caught, then the process
exits.

> 2) sys.exit is called from another thread but while this thread
> is in context manager?

Then the other thread raises SystemExit, and the current thread is
unaffected.  sys.exit only affects the thread it is called in.

You can certainly come up with scenarios in which the finally clause
does not execute, e.g. killing the interpreter with "kill -9" or
yanking out the power cord.  Within the confines of the Python
interpreter, though, it is guaranteed that the finally block will
execute.

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