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.

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to