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


Groups > comp.lang.python > #103967

Re: yield in try/finally case

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Random832 <random832@fastmail.com>
Newsgroups comp.lang.python
Subject Re: yield in try/finally case
Date Thu, 03 Mar 2016 10:12:19 -0500
Lines 41
Message-ID <mailman.150.1457017942.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>
Mime-Version 1.0
Content-Type text/plain
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de nQ35EC0J9IjxialAtLRRRw/2E5hugR6HlObS7tfCNNqA==
Return-Path <random832@fastmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'sys': 0.05; 'lambda:': 0.09; 'received:internal': 0.09; 'def': 0.13; 'explicitly': 0.15; "hasn't": 0.15; 'thu,': 0.15; 'hams': 0.16; 'message- id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:66.111.4.27': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:out3-smtp.messagingengine.com': 0.16; 'received:psf.io': 0.16; 'subject:case': 0.16; 'subject:yield': 0.16; 'wrote:': 0.16; 'either.': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'yield': 0.27; 'skip:u 20': 0.28; '---': 0.28; 'subject:/': 0.30; 'run': 0.33; 'foo': 0.33; 'open': 0.33; 'but': 0.36; "wasn't": 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'received:66': 0.38; 'files': 0.38; 'end': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'header:Message-Id:1': 0.61; 'mar': 0.65; '**kw)': 0.84; '**kw):': 0.84; 'assertion.': 0.84; 'otten': 0.84; 'subject:try': 0.84
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=TCKyAYA7ndq9G8UGmVrUlY8lOR8=; b=ZwdeGC jB+lOs7nHxffxZ7tueS8MhDxxGlwCBUl66YVUZU4O9M8IGKIT2LkqFREddtt8HoW NMCB5XrFqXuk7jvtRInTFTX76DKFHf6ZpXkXv+rcBvuBE75YvIs/BDESR7Xk/8bR tgLBn/gaq0kfdtn8PwWXSRGwbdLGcYKwFaI9I=
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=TCKyAYA7ndq9G8U GmVrUlY8lOR8=; b=Y23Ikdh1DB6pjtB+VNnyat5FTXWK/iM7pGr5vB7IbzuNScd jGHBvTtgslLtRM12yr++m0dWSXxoUBjGG5ah8BGoXah6Xs2YKxIt7yo4z4y5J+d9 /1A+ZE+HlN7uHcACh/vwVZjH/9443L3Abia9XUk3YQp1JrdAOTOQFlomV4zg=
X-Sasl-Enc k44vMRAeYsm+O5nJ0fIEWlRWGevDpm47uG4nwwjoPLeP 1457017939
X-Mailer MessagingEngine.com Webmail Interface - ajax-b8d6474a
In-Reply-To <nb9f9r$ngi$1@ger.gmane.org>
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:103967

Show key headers only | View raw


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.

import sys

_open = open
files = []

def myclose(self):
    print("--- closed " + self.name)
    self._close()

def open(*args, **kw):
    f = _open(*args, **kw)
    f._close = f.close
    f.close = lambda: myclose(f)
    files.append(f)
    return f

def upperfile(filename):
    with open(filename) as f:
        for line in f:
            yield line.upper()

for uf in map(upperfile, sys.argv[1:]):
    for line in uf:
        print(line, end="")
        break

print("--- end of program")

====

FOO
--- closed tmp1.txt
HAMS
--- end of program
--- closed tmp2.txt

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