[Python-Dev] New and Improved Import Hooks
Gordon McMillan
gmcm@hypernet.com
Thu, 5 Dec 2002 08:46:37 -0500
On 5 Dec 2002 at 9:07, Moore, Paul wrote:
> I read Gordon's comments as the latter, which
> implies your approach (A) below.
Only the first sentence of (A). Having things that
look like strings but aren't on sys.path just means
that you have to convert PYTHONPATH, and trap
modifications to sys.path. Why bother? There's
an easier way.
[Just]
> > A) Stick with strings. Hooks can be implemented by
> > subclassing str.
[...]
> > B) Allow arbitrary objects on sys.path.
[...]
> > Regarding PYTHONPATH and
> > sys.path.append("/path/to/my/archive.zip"): for now
> > I'd suggest that the sys.path traversing code checks
> > for a .zip extension, and replace the item with a
> > zipimporter instance. This check can be very cheap.
> > Later we could add a general extension-checking
> > feature, where one could register an import hook for
> > a specific extension. This might be a case of YAGNI,
> > though...
Here's what iu.py does:
The first time an entry on sys.path requires
examination, the list of importers is traversed
until one accepts it. That importer is associated
with the name, so subsequent imports are only
one dict lookup away from the importer.
Search order is as expected (sys.path.insert(0, ...)
works). If your app only uses stuff from sys.path[0],
no time is wasted finding the right importers for
sys.path[1:].
-- Gordon
http://www.mcmillan-inc.com/