[Import-SIG] PEP 382 as an import hook

P.J. Eby pje at telecommunity.com
Mon Jun 27 20:41:20 CEST 2011


At 10:07 AM 6/27/2011 +0200, Martin v. Löwis wrote:
>Am 27.06.2011 04:52, schrieb Eric Snow:
> > Would a PEP 302 import hook to implement PEP 382 be too inefficient?
> > What about just for testing out the PEP?
>
>I think everybody agrees that it would be desirable to have it - it's
>just that nobody has managed so far to implement, as it is really really
>difficult.

It's not *that* difficult.  You just need to use a meta hook, and pay 
some performance overhead.

The overhead comes in two places: first, you have to do an extra 
isdir() (or trap an exception from listdir()), and second, there's 
extra overhead before builtin/frozen imports, or before an 
ImportError is raised for missing modules.

If you are willing to pay those overheads (and the necessary extra 
code to support namespace checking for the builtin importer types), a 
sys.meta_path hook should be sufficient.  (Per my proof-of-concept.)



More information about the Import-SIG mailing list