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


Groups > comp.lang.python > #18837

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!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed20.multikabel.net!amsnews11.chello.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'cpython': 0.05; 'so?': 0.05; 'none:': 0.07; 'python': 0.08; 'called.': 0.09; 'decorator': 0.09; 'executed': 0.09; 'files:': 0.09; 'finally:': 0.09; 'generators': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'statement.': 0.09; 'underlying': 0.09; 'api': 0.09; 'def': 0.13; 'case.': 0.15; 'clause.': 0.16; 'connections.': 0.16; 'enigma': 0.16; 'hurtful': 0.16; 'ironpython,': 0.16; 'kern': 0.16; 'wrote:': 0.18; 'yet.': 0.18; 'appears': 0.19; 'trying': 0.21; 'stuff': 0.22; 'header:In-Reply- To:1': 0.22; 'pep': 0.23; 'suggests': 0.23; 'code': 0.25; 'helpful': 0.26; "i'm": 0.26; 'interpret': 0.29; 'true,': 0.29; 'yield': 0.29; 'pm,': 0.29; 'closing': 0.30; 'email name:': 0.30; 'semantics': 0.30; 'header:User-Agent:1': 0.33; 'this.': 0.33; 'header:X-Complaints-To:1': 0.33; 'object': 0.33; 'to:addr:python- list': 0.34; 'probably': 0.34; 'someone': 0.34; 'anything': 0.34; "we're": 0.34; 'algorithms': 0.34; 'statement,': 0.34; 'try:': 0.34; 'running': 0.35; 'subject:/': 0.35; 'however,': 0.36; 'similar': 0.36; 'connection': 0.36; 'except': 0.37; 'reference': 0.37; 'could': 0.37; 'received:org': 0.38; 'should': 0.39; 'goes': 0.39; 'why': 0.39; 'called': 0.40; "it's": 0.40; 'to:addr:python.org': 0.40; 'might': 0.40; 'one,': 0.40; 'once': 0.60; 'design': 0.61; 'your': 0.61; 'world': 0.62; 'our': 0.64; 'guarantee': 0.65; 'believe': 0.65; 'resources,': 0.67; 'soon': 0.70; 'exhaust': 0.84; 'generator,': 0.84; 'guarantee.': 0.84; 'scarce': 0.84; 'subject:try': 0.84; 'eco': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robert Kern <robert.kern@gmail.com>
Subject Re: contextlib.contextmanager and try/finally
Date Wed, 11 Jan 2012 17:14:40 +0000
References <10841855.1646.1326296715536.JavaMail.geo-discussion-forums@yqiz15>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host81-159-163-144.range81-159.btcentralplus.com
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0) Gecko/20111105 Thunderbird/8.0
In-Reply-To <10841855.1646.1326296715536.JavaMail.geo-discussion-forums@yqiz15>
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.4655.1326302094.27778.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1326302094 news.xs4all.nl 6951 [2001:888:2000:d::a6]:42206
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18837

Show key headers only | View raw


On 1/11/12 3:45 PM, johannh@gmail.com wrote:
> I'm trying to write a context manager to handle database connections, under the principle that I should not rely on CPython's reference-counting semantics to clean up scarce resources, like connections.
>
> I wrote:
>
> @contexlib.contextmanager
> def ensure_connection(con=None):
>      con_created = False
>      if con is None:
>          con_created, con = True, make_connection()
>      try:
>          yield con
>      finally:
>          if con_created:
>              con.close()
>
> 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?

Looking at the paragraph before this one, it appears that the PEP is talking 
about the .close() method on generators, which is really just a general purpose 
API for closing generators that might not be exhausted yet. It's not really 
related to the context manager stuff except that it came up during the design 
process of the context manager along with the related .send() and .throw() methods.

__enter__() will call .next() once to execute the code up to the yield 
statement. Then __exit__() will call .next() once again to execute the code 
after the yield statement, including the finally: clause. That's the only thing 
you need to rely on. Your connection-closing code will be called if __exit__() 
gets called. That will exhaust your generator, so the .close() method will not 
really do anything helpful or hurtful in such a case.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

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