[Python-Dev] New and Improved Import Hooks

M.-A. Lemburg mal@lemburg.com
Thu, 05 Dec 2002 11:51:52 +0100


Just van Rossum wrote:
> M.-A. Lemburg wrote:
> 
> 
>>Why not leave sys.path as list of ordinary strings and add
>>all the extra support to the existing import mechanism via
>>hooks at the right places ?!
>>
>>E.g. such a hook may scan sys.path for files which end
>>in '.zip' and then redirect the lookups into their ZIP
>>archive directory.
> 
> 
> Then you get the problem that Samuele pointed out: if the *hook* scans sys.path
> you get a funny path resolution order: hooks[0] may find the module on
> sys.path[3] before hooks[2] finds it on sys.path[2].

Uhm, I was unclear here I guess... I meant that the strings
in sys.path should trigger a hook which then takes care of
managing the lookup and possibly the import. Much like what
urllib does with URLs.

Here's a sketch:

1. User programs register import hooks based on REs which are
    used to match the entries in sys.path, e.g. ".*\.zip" for
    ZIP importers (caching could help in improving the mapping
    performance).

2. When Python sees an import request, it scans sys.path and
    creates hook objects for each entry which it then calls
    to say "go look and check whether you have module X" until
    one of the hooks succeeds.

3. Python then uses the hook object to complete the import
    in much a similar way as e.g. SAX parsers call out to
    event handlers.

The idea is to reuse as much of the existing import machinery
as possible -- writing these hooks in C wouldn't be too hard
either.

> We should document that
> 
> - sys.path *may* contain arbitrary objects, and that sys.path traversing
>   code should be careful.
> - the std library promises to not add objects to sys.path that are not
>   isinstance(p, (str, unicode)).
> 
> I think this is the perfect compromise. Thank you </F>!

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
_______________________________________________________________________
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting:                               http://www.egenix.com/
Python Software:                    http://www.egenix.com/files/python/