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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Wed, Jan 11, 2012 at 8:45 AM, wrote: > However, then I read the following paragraph from PEP-343: > > =A0 =A0Note that we're not guaranteeing that the finally-clause is > =A0 =A0executed immediately after the generator object becomes unused, > =A0 =A0even though this is how it will work in CPython. =A0This is simila= r > =A0 =A0to auto-closing files: while a reference-counting implementation > =A0 =A0like CPython deallocates an object as soon as the last reference > =A0 =A0to it goes away, implementations that use other GC algorithms do > =A0 =A0not make the same guarantee. =A0This applies to Jython, IronPython= , > =A0 =A0and probably to Python running on Parrot. > > That suggests that I cannot rely on the contextlib.contextmanager decorat= or to ensure that the connection is closed and would have to write my own o= bject with __enter__ and __exit__ methods to guarantee this. > > Is this understanding accurate? =A0If so, could someone illustrate why th= is 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.