[Python-ideas] PEP: Extended stat_result (First Draft)

Pieter Nagel pieter at nagel.co.za
Mon May 6 19:49:44 CEST 2013


On Mon, 2013-05-06 at 20:58 +0400, Oleg Broytman wrote:

>    I don't like the idea of changing os.stat() behaviour.

I'm actually not proposing changing os.stat() behaviour, not for
existing code. I'm proposing new optional behaviour that code could
request.

There is precedent for functions not raising exceptions, depending on
the way they're called. dict.pop will raise KeyError, unless a default
is given. str.encode can raise UnicodeErrors, depending on the value of
the 'errors' parameter.

In that light, a function that might or might not raise
FileNotFoundError depending on how it was called is not that weird.

The motivation is to make "performant" code that calls stat() once look
more similar to naive code that potentially calls it many times. And if
naive code can say "os.path.isfile(f)" and get False even when f does
not exist, then performant code should be able to do something similar
by just statting and then interogating the results, without adding
exception handling.

> If you want to have a different type of return value use a different
> function. Call it os.stat_ex() or something.

This is more feasible nowadays, now that there's the pattern of
specifying file descriptors as path, and passing follow_symlinks.

That means I won't need to do os.fstat_ex() and os.lstat_ex() as well.

-- 
Pieter Nagel





More information about the Python-ideas mailing list