[Python-Dev] an alternative to embedding policy in PEP 418 (was: PEP 418: Add monotonic clock)

R. David Murray rdmurray at bitdance.com
Thu Apr 5 17:38:13 CEST 2012


On Thu, 05 Apr 2012 19:22:17 +0400, Oleg Broytman <phd at phdru.name> wrote:
> On Thu, Apr 05, 2012 at 11:45:06PM +0900, Stephen J. Turnbull wrote:
> > On Thu, Apr 5, 2012 at 10:34 PM, Oleg Broytman <phd at phdru.name> wrote:
> > >   Why doesn't open() return None for a non-existing file? or
> > > socket.gethostbyname() for a non-existing name?
> > 
> > That's not an answer to my question, because those calls have very
> > important use cases where the user knows the object exists (and in
> > fact in some cases open() will create it for him), so that failure to
> > exist is indeed a (user) error (such as a misspelling).  I find it
> > hard to imagine use cases where "file = open(thisfile) or
> > open(thatfile)" makes sense.  Not even for the case where thisfile ==
> > 'script.pyc' and thatfile == 'script.py'.
> 
>    Counterexamples - any configuration file: a program looks for its config
> at $HOME and not finding it there looks in /etc. So
>     config = open('~/.someprogram.config') or open('/etc/someprogram/config')
> would make sense. The absence of any of these files is not an error at
> all - the program just starts with default configuration. So if the
> resulting config in the code above would be None - it's still would be
> ok. But Python doesn't allow that.
>    Some configuration files are constructed by combining a number of
> user-defined and system-defined files. E.g., the mailcap database. It
> should be something like
>     combined_database = [db for db in (
>         open('/etc/mailcap'),
>         open('/usr/etc/mailcap'),
>         open('/usr/local/etc/mailcap'),
>         open('~/.mailcap'),
>     ) if db]
> But no way - open() raises IOError, not return None. And I think it is
> the right way. Those who want to write the code similar to the examples
> above - explicitly suppress exceptions by writing wrappers.

Ah, but the actual code in the mimetypes module (whose list is even
longer) looks like this:

    for file in files:
        if os.path.isfile(file):
            db.read(file)

That is, Python provides a query function that doesn't raise an error.

Do you really think we need to add a third clock function (the query
function) that just returns True or False?  Maybe we do, if actually
creating the clock could raise an error even if exists, as is the case
for 'open'.

(But unless I'm confused none of this has anything to do with Victor's
PEP as currently proposed :)

--David


More information about the Python-Dev mailing list