[Python-Dev] New Import Hooks PEP, a first draft (and req. for PEP #)

Just van Rossum just@letterror.com
Fri, 20 Dec 2002 23:19:30 +0100


James C. Ahlstrom wrote:

> > I didn't change anything there and I sure don't break any code,
> > because there's hardly any code out there that expects
> > imp.find_module() to work for zip files <0.5 wink>.
> 
> Huh?  In my implementation imp.find_module() imp.load_module()
> work for zip files. 

I know, but this is only possible when the import extension is hard
coded in import.c, and lifting the need for hard coded hooks in import.c
was the entire point of my exercise. This comes at the cost of it not
working with imp.find_module().

> You mean they don't work to provide access to data files, right?

Misunderstanding, I meant what I said. In my implementation
imp.find_module() *doesn't* work for zip files, but I don't think that's
such a big deal as zipimport is a new thing, or rather, the new import
hooks are, erm, new. You've hit a weak spot in my scheme though, and
perhaps the PEP should make that clearer. Nevertheless I see no decent
way to integrate the importer protocol with imp.find_module/load_module.

The way I see it (as I tried to say in the PEP) is that
imp.find_module/load_module interfaces the builtin import mechanism as
it is in Python 2.2 and before (builtin, frozen and plain directory
imports, that's it). imp.load_module() is a primitive dispatcher that
switches on the filetype enum field of a filedescr struct. What is
needed with the new hooks is that the load_module() of a importer/loader
object be called. This is a modernization (OO-ification) of the same
idea, but obviously not compatible with the current imp module's world
view. Hence my proposal to add a new function called imp.find_module2(),
which also takes into account that the importer protocol needs the
*full* module name -- another (even more unfixable, I think)
incompatibility with imp.find_module().

(Btw. the only uses in the standard library of imp.find_module or
imp.load_module are in ihooks.py imputil.py, pyclbr.py and pydoc.py.
It's not like it's an insanely popular API, and I stick with my claim
that I didn't break anything ;-)

Just