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


Groups > comp.lang.python > #103974

Re: yield in try/finally case

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: yield in try/finally case
Date Thu, 03 Mar 2016 17:20:02 +0100
Organization None
Lines 34
Message-ID <mailman.153.1457022013.20602.python-list@python.org> (permalink)
References <84965b86-819b-4924-bca9-e82eed040606@googlegroups.com> <mailman.146.1457007243.20602.python-list@python.org> <de3f3492-063d-469c-8ad2-c93098335a2f@googlegroups.com> <nb9f9r$ngi$1@ger.gmane.org> <1457017939.1829117.538575946.054CBB8F@webmail.messagingengine.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de RdI1hLUloLuS5hZhFHM6sAhIsHvPcAqbMU45QHU0gLYw==
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.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'exiting': 0.09; 'garbage': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'exception': 0.13; 'def': 0.13; 'explicitly': 0.15; "hasn't": 0.15; 'thu,': 0.15; 'nevertheless': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:case': 0.16; 'subject:yield': 0.16; 'wrote:': 0.16; 'pointed': 0.18; 'either.': 0.22; "python's": 0.23; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'moved': 0.27; 'yield': 0.27; 'actual': 0.28; 'behaviour': 0.29; 'subject:/': 0.30; 'normally': 0.30; 'writes': 0.30; 'run': 0.33; 'surprised': 0.33; 'fail': 0.35; 'but': 0.36; "wasn't": 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'someone': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'collection': 0.60; 'your': 0.60; 'skip:u 10': 0.61; 'course': 0.62; 'mar': 0.65; 'assertion.': 0.84; 'oscar': 0.84; 'otten': 0.84; 'strategy.': 0.84; 'subject:try': 0.84; 'triggering': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8dae.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:103974

Show key headers only | View raw


Random832 wrote:

> On Thu, Mar 3, 2016, at 08:47, Peter Otten wrote:
>> This is because the last generator uf = upperfile(...) is not garbage-
>> collected and wasn't explicitly closed either.
> 
> But the program hasn't ended yet when you run your assertion.

If your expectations are in line with Python's actual behaviour -- then 
fine. Normally someone who writes

with acquire_resource() as r:
    use(r)
assert r was released

wants the resource to be released when the with suite is left.

When the with-statement is moved into a generator

def gen_resource():
    with acquire_resource() as r:
        yield r

for r in gen_resource():
    use(r)
    break # use(r) triggering an exception would have the same effect
assert r was released # may fail

you are at the mercy of the Python interpreter's garbage collection 
strategy. 

Of course you are exiting the for-suite, not the with-suite. Nevertheless 
this surprised me when Oscar pointed it out the first time.

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


Thread

yield in try/finally case 刘琦帆 <lqf.txx@gmail.com> - 2016-03-03 03:52 -0800
  Re: yield in try/finally case Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-03-03 12:13 +0000
    Re: yield in try/finally case 刘琦帆 <lqf.txx@gmail.com> - 2016-03-03 04:52 -0800
      Re: yield in try/finally case Peter Otten <__peter__@web.de> - 2016-03-03 14:47 +0100
      Re: yield in try/finally case Random832 <random832@fastmail.com> - 2016-03-03 10:12 -0500
      Re: yield in try/finally case Peter Otten <__peter__@web.de> - 2016-03-03 17:20 +0100
      Re: yield in try/finally case Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-03-03 16:24 +0000
  Re: yield in try/finally case Random832 <random832@fastmail.com> - 2016-03-03 10:00 -0500
    Re: yield in try/finally case Steven D'Aprano <steve@pearwood.info> - 2016-03-04 02:57 +1100

csiph-web