[Python-Dev] New Import Hooks PEP, a first draft (and req. for
PEP #)
James C. Ahlstrom
jim@interet.com
Fri, 20 Dec 2002 12:08:07 -0500
Just van Rossum wrote:
> PEP: XXX
> Title: New Import Hooks
First, I commend Just and Paul for their work. It is not easy
sloughing through Python imports. And the background on how
imports work and the desired changes are valuable. I am the
author of the competing PEP 273 implementation, and if Just's
implementation can provide a better implementation, I won't
mind.
But the changes to the imp module IMHO are ill advised. I am
posting an alternative here, and will post other objections
separately.
> Integration with the 'imp' module
>
> The new import hooks are not easily integrated in the existing
> imp.find_module() and imp.load_module() calls. It's questionable
> whether it's possible at all without breaking code; it is better to
> simply add a new function to the imp module.
Although I originally found the find/load dichotomy annoying,
I now think it should be kept. It solves the real
world problem of finding files which are not Python modules.
Currently Zope and others find configuration and data files
either by looping over sys.path themselves, or by looking
at the base name of module.__file__ (the file name of an
imported module). Both fail for zip imports.
I too find the imp find/load function signatures hard to love. But
I also don't want to break any code which depends on them, nor
make gratuitous changes which make them useless for zip imports.
I think the imp module should Just Work for zip imports.
I suggest we keep imp.find_module but add another argument:
find_module(name [, path [, suffixes]])
where suffixes defaults to imp.get_suffixes(). This enables it
to be used to search for files with different suffixes, for
example suffixes=[".conf"].
To make this work, the returned file object would have to be
either a real file object, or another object with a read() method.
Python has lots of precedents for accepting file-like objects. For
example, sys.stdout can be replaced with any object which has a
write method.
The returned file-like object must have a read() method and
a close() method. It could also have a stat() method if it
is a zip file, because zip files record the file date, time
and size.
So you call file.read() for a configuration file, or pass
it to imp.load_module() if it is a Python module.
JimA