Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103975
| Path | csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: yield in try/finally case |
| Date | Thu, 3 Mar 2016 16:24:38 +0000 |
| Lines | 55 |
| Message-ID | <mailman.154.1457022305.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=UTF-8 |
| X-Trace | news.uni-berlin.de eMHjcm4PB7DSY1IQIibGNgW5Up9UY8CNDk1Ip8tF2h8A== |
| Return-Path | <oscar.j.benjamin@gmail.com> |
| 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; 'anyway.': 0.04; 'context': 0.05; 'sys': 0.05; 'exit': 0.07; 'pypy': 0.07; 'cc:addr:python- list': 0.09; 'exit.': 0.09; 'lambda:': 0.09; 'def': 0.13; 'explicitly': 0.15; "hasn't": 0.15; 'thu,': 0.15; '2016': 0.16; '__del__': 0.16; 'cc:name:python list': 0.16; 'hams': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:case': 0.16; 'subject:yield': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'either.': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'message- id:@mail.gmail.com': 0.27; 'yield': 0.27; 'skip:u 20': 0.28; '---': 0.28; 'subject:/': 0.30; 'code': 0.30; 'supposed': 0.31; 'probably': 0.31; 'run': 0.33; 'point': 0.33; 'achieving': 0.33; 'foo': 0.33; 'open': 0.33; 'file': 0.34; 'received:google.com': 0.35; 'done': 0.35; 'but': 0.36; 'received:209.85': 0.36; "wasn't": 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'files': 0.38; 'end': 0.39; 'why': 0.39; 'your': 0.60; 'close': 0.61; "you'll": 0.61; 'skip:u 10': 0.61; 'fire': 0.63; 'march': 0.64; 'mar': 0.65; '**kw)': 0.84; '**kw):': 0.84; 'assertion.': 0.84; 'oscar': 0.84; 'otten': 0.84; 'subject:try': 0.84; 'reasoning': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=Kie4zLBkOUpusYqIAqIs7WZsMclsH5ydfZea0v+MiQs=; b=opMFx6rd65XZE6UyO/owC6ZcP6226VwndfcMoeWiPR+pMyd4o6h/lwPxc7NzwrBcGd jayMbg3XUS1vinidmblKzlSVFYUKJSTbyvYoLsJ1aOsxEBoYnw+swbX73sko9hlnIxuj c8VGjXdyk+cQug/TZb9/v2bIO+8ab8+e5CXGo+csTcqDUuGmnk2byGiERnaTn9HM5vWd 6fx90W5BsdCC4HE+ePcUNijATfKAGRN+jVF5dNaY49oujphzW4P3QKsiPjPkZc4111kK qDo0WJUDNQz8BHk/sMrUgArwBRVJScQ5ZqnLCNL/eeMXqlB5AYKxOFFe2mqq/sh4WJAr 9Kww== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=Kie4zLBkOUpusYqIAqIs7WZsMclsH5ydfZea0v+MiQs=; b=N8ng+Z3O3aiRKc1APfsEySMA4XRsIMtMVbpdonNR2tGj7CNcNjyKvJWbuxc+4Y2zQ+ ZNPmQ0nawajqh6MGZ3bVCocZ6FAfgo5kobni00DM2itvMHy1AJBECr/NvjDIgchFyK2B b9U2hbGnsgCO7Wtt8U6WqAsMxJbXS9gXcI+TB5a6y4lFjomZVtuW6YZp+ZQxY7Dr/uDI IUWeb3iHlxsT3P4lXYPsUT4sZvWt3y2ZGGZ1anNbS82/B3cXRfb3zn1ASmX/KqWCn3oP 6Vdq/aEu6UTaEQaNfzHZ+qYRE3rs7mBQ5RAlxUxg160OP0mPocOG/8z29Ls9y+wS6rHk zEHQ== |
| X-Gm-Message-State | AD7BkJLkSJy6+06PoDrNCaB0ei6avV5l/Iu6Q+0N2Tce9EURlee0HOtuomNk0MyF2t5JHtG//ejtqUO9Fo3Phg== |
| X-Received | by 10.112.235.71 with SMTP id uk7mr1340370lbc.39.1457022297896; Thu, 03 Mar 2016 08:24:57 -0800 (PST) |
| In-Reply-To | <1457017939.1829117.538575946.054CBB8F@webmail.messagingengine.com> |
| 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:103975 |
Show key headers only | View raw
On 3 March 2016 at 15:12, Random832 <random832@fastmail.com> 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.
>
> 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
If you're happy letting __del__ close the file then why bother with
the context manager in the first place? By the reasoning above we
don't even need to close the file in __del__ since all open files get
closed at process exit anyway. The point is that finally/__exit__ are
not achieving what they are supposed to achieve: the guarantee that
the file is immediately closed when you're done with it.
Try your code under pypy and you'll probably find that your context
manager doesn't fire even at process exit.
--
Oscar
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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