[Python-Dev] New and Improved Import Hooks

M.-A. Lemburg mal@lemburg.com
Thu, 05 Dec 2002 13:00:25 +0100


Martin v. L=F6wis wrote:
> "M.-A. Lemburg" <mal@lemburg.com> writes:
>=20
>=20
>>But why store the import objects in sys.path and not in
>>some auxilliary dictionary instead ?
>=20
>=20
> What would be the advantage of doing so?

Real strings in a real dictionary.

> And, if this is done: How can I add my own custom import mechanism to
> sys.path? The machinery to replace strings in sys.path is only
> available for zip files, all other hooks must add their objects to
> sys.path directly.

Well, that's what I tried to solve in the sketch I posted earlier
(slightly modified to meet Fredrik's requirements):

1. User programs register import hooks based on suffixes which are
    used to match the entries in sys.path, e.g. ".zip" for
    ZIP importers (caching could help in improving the mapping
    performance; this is where the auxilliary dictionary comes into
    play).

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.

--=20
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/