Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Random832 Newsgroups: comp.lang.python Subject: Re: yield in try/finally case Date: Thu, 03 Mar 2016 10:12:19 -0500 Lines: 41 Message-ID: References: <84965b86-819b-4924-bca9-e82eed040606@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de nQ35EC0J9IjxialAtLRRRw/2E5hugR6HlObS7tfCNNqA== Return-Path: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103967 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