[Python-Dev] New and Improved Import Hooks

Jack Jansen Jack.Jansen@cwi.nl
Wed, 4 Dec 2002 11:24:38 +0100


On Tuesday, Dec 3, 2002, at 21:05 Europe/Amsterdam, Just van Rossum 
wrote:
> Regarding sys.import_hooks, I see three possibilities:
>
> #1
>     for p in sys.path:
>         for hook in sys.import_hooks:
>             hook(p, ...)
>
> #2
>     for hook in sys.import_hooks:
>         for p in sys.path:
>             hook(p, ...)
>
> #3
>     for hook in sys.import_hooks:
>         # the hook can traversing sys.path if needed.
>         hook(...)

Note that there is "prior art" for case 1: the PYC resource importer 
for MacPython, which also
looks at files (in stead of directories) on sys.path.

It does have a bit of hairy code so it skips sys.path entries that 
don't apply to it
(directories, basically). This was a major speedup, so maybe this 
should be generalized
to something like

	for p in sys.path:
		for hook in sys.import_hooks:
			if not hook_can_handle.has_key((hook, p)):
				hook_can_handle[(hook, p)] = hook(p, None) # None requests 
"compatibility test"
			if hook_can_handle[(hook, p)]:
				hook(p, ....)
--
- Jack Jansen        <Jack.Jansen@oratrix.com>        
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- Emma 
Goldman -