[Python-ideas] With clauses for generator expressions
Andrew Barnert
abarnert at yahoo.com
Fri Nov 16 10:26:03 CET 2012
From: Steven D'Aprano <steve at pearwood.info>
Sent: Thu, November 15, 2012 10:05:36 PM
> As far as the given use-case is concerned:
>
> upperlines = (line.upper() for line in open('foo'))
>
> I don't see what the concern is. The file will remain open so long as the
> generator is not exhausted, but that has to be the case no matter what you
> do. If the generator is thrown away before being exhausted, the file will
> eventually be closed by the garbage collector, if only when the application
> or script exits. For short-lived scripts, the temporarily leakage of a file
> handle or two is hardly likely to be a serious problem.
>
> Presumably if you have a long-lived application with many such opened
> files, you might risk running out of file handles when running under Jython
> or IronPython. But I think that's a sufficiently unusual and advanced use-
> case that I'm not worried that this is a problem that needs solving with
> syntax instead of education.
This seems to be an argument against with statements, or any other kind of
resource management at all besides "trust the GC". I'm pretty sure PEP 310, PEP
340, PEP 343, and the discussion around them already had plenty of
counter-arguments, but here's a couple quick ones: If you've opened a file for
exclusive access (the default on Windows), you can't safely open it again if you
can't predict when it will be closed. If the context in question is a mutex lock
rather than a file open, you can't safely lock it again if you can't predict
when it will be released (and, even if you never want to lock it again, you
could end up deadlocked against another thread that does).
More information about the Python-ideas
mailing list