[Python-ideas] Add create mode to open()
jeanpierreda at gmail.com
Wed Aug 17 06:13:17 CEST 2011
On Tue, Aug 16, 2011 at 11:49 PM, Benjamin Peterson <benjamin at python.org> wrote:
> Devin Jeanpierre <jeanpierreda at ...> writes:
>> LBYL is impossible here without a race condition, in fact, because the
>> situation can change between looking and leaping. An exception, or
>> else return code, is the only way. These can be checked for after the
> How often in this used? In every application I've written, writing a file
> usually results from the user giving a path, in which case it's intended to
> replace whatever is already there.
Unless it isn't. Most GUI apps ask you to confirm whether you want to
open a file even where one exists. Suppose you do a LBYL approach: you
check to see if no file is there, then somebody writes a file there,
then you overwrite it because you used 'w' mode because no file was
there. It's not a disaster, since this is kind of hard to do by
accident, but it is incorrect behavior if you wanted to actually ask
if overwriting was kosher.
On the other hand, if you ask _after_ trying to open the file, then
one of two things can happen: the user says "abort", or the user says,
"overwrite it". In the former case, we start over. In the latter case,
the only remaining race condition is one that doesn't matter: the file
might disappear before you overwrite it!
>> I'd also point out that for those that don't want race conditions,
>> Python is discouraging. The correct incantation involves two
>> undocumented constants, plus a unique and rarely used way of opening
>> files that involves unix file descriptors.
> If you truly want to avoid all filesystem race conditions, you're going to be
> dealing with file descriptors and low-level syscalls galore. Moving one aspect
> to a higher level is not too helpful on the whole.
Well, eh, not really. As far as I know this particular primitive is
the probably the most important one. It's certainly the only one I've
ever wanted to use
More information about the Python-ideas