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


Groups > comp.lang.python > #18839

Re: contextlib.contextmanager and try/finally

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.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.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'mentioned,': 0.04; 'cpython': 0.05; 'so?': 0.05; 'fashion.': 0.07; 'python': 0.08; 'decorator': 0.09; 'executed': 0.09; 'files:': 0.09; 'generators': 0.09; 'am,': 0.12; 'algorithm': 0.13; 'closes': 0.16; 'ironpython,': 0.16; 'runs.': 0.16; 'try-finally': 0.16; '\xa0this': 0.16; '\xa0to': 0.16; '(i.e.': 0.17; 'wed,': 0.17; 'wrote:': 0.18; 'issue.': 0.19; 'jan': 0.19; 'written': 0.20; 'header:In-Reply-To:1': 0.22; 'clause': 0.23; 'referring': 0.23; 'resumes': 0.23; 'suggests': 0.23; '\xa0if': 0.23; 'received:74.125.82.174': 0.24; 'timely': 0.25; 'message- id:@mail.gmail.com': 0.28; 'explicitly': 0.29; 'yield': 0.29; 'generally': 0.30; 'construct': 0.30; 'second,': 0.30; 'syntax,': 0.30; 'yields': 0.30; 'changes': 0.32; 'this.': 0.33; 'there': 0.33; 'object': 0.33; 'to:addr:python-list': 0.34; 'probably': 0.34; 'someone': 0.34; "we're": 0.34; 'algorithms': 0.34; 'received:74.125.82': 0.35; 'running': 0.35; 'subject:/': 0.35; 'however,': 0.36; 'similar': 0.36; 'connection': 0.36; 'but': 0.37; 'reference': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'some': 0.38; 'goes': 0.39; 'why': 0.39; 'called': 0.40; 'to:addr:python.org': 0.40; 'within': 0.60; 'more': 0.61; 'your': 0.61; 'happen': 0.61; 'illegal': 0.63; 'guarantee': 0.65; 'believe': 0.65; '2012': 0.67; '11,': 0.68; 'soon': 0.70; 'guaranteed': 0.77; 'guarantee.': 0.84; 'passage': 0.84; 'subject:try': 0.84; 'immediately,': 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 :content-type:content-transfer-encoding; bh=8dEPatQm08Hh6fJQI+VOHdj9p7SybtDD5Ouvr415Wy4=; b=ZNl5u/PEHY8nqREqF99Li2Awh3nYlIC2plkr9qmIc3M7qF+n1uVTEE89Bj8T0uu7CQ o7l8qFxYpScJECWp8GQoa6BcfUQDdaRtf1a3QTdWVSsqetToFnRSxoQjU3A4wpJ31wLF p5Lj7yk5o3I2LxleQYlds1ir3GqgoDuel3qgk=
MIME-Version 1.0
In-Reply-To <10841855.1646.1326296715536.JavaMail.geo-discussion-forums@yqiz15>
References <10841855.1646.1326296715536.JavaMail.geo-discussion-forums@yqiz15>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 11 Jan 2012 10:20:19 -0700
Subject Re: contextlib.contextmanager and try/finally
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
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.4658.1326302452.27778.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1326302452 news.xs4all.nl 6980 [2001:888:2000:d::a6]:47127
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18839

Show key headers only | View raw


On Wed, Jan 11, 2012 at 8:45 AM,  <johannh@gmail.com> wrote:
> However, then I read the following paragraph from PEP-343:
>
>    Note that we're not guaranteeing that the finally-clause is
>    executed immediately after the generator object becomes unused,
>    even though this is how it will work in CPython.  This is similar
>    to auto-closing files: while a reference-counting implementation
>    like CPython deallocates an object as soon as the last reference
>    to it goes away, implementations that use other GC algorithms do
>    not make the same guarantee.  This applies to Jython, IronPython,
>    and probably to Python running on Parrot.
>
> That suggests that I cannot rely on the contextlib.contextmanager decorator to ensure that the connection is closed and would have to write my own object with __enter__ and __exit__ methods to guarantee this.
>
> Is this understanding accurate?  If so, could someone illustrate why this is so?

First, this is just a timing issue.  There is no guarantee that the
finally clause will be executed immediately, but it is guaranteed to
be executed at some point.  In the other implementations, it would
happen whenever the GC algorithm runs.

Second, I believe that passage is not referring to the contextmanager
decorator specifically, but more generally to the changes that were
made to allow generators to yield from within a try-finally construct
(previously this would have been illegal syntax, since there was no
way to guarantee the finally block would be performed).

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.

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