[Python-Dev] Another approach for the import mechanism

Brian Quinlan brian@sweetapp.com
Thu, 05 Dec 2002 16:14:53 -0800


> Of course, if somebody wants to provide a crypt importer (which
> decrypts the source before importing it) in addition to the
> pybz2importer, then you get the same coordination problem as with all
> prior import hooks.

Does anybody really believe that this problem can be solved in general?

I like the following:

1. only strings representing directories are allowed in sys.path 
  (a zip archive is conceptually a directory)
2. there are two types of hook that can be installed:
     - an import hook that returns a module object, a stream object
       or None
     - a stream hook that has the opportunity to return a different
       stream if it likes

When the import hook returns a stream, every stream hook is given a
chance to transform that stream.  If a stream hook returns a transformed
stream then every other hook is called again. This continues until no
stream hook is interested in transforming the stream anymore. 

I know that this might have performance issues, but it seems like it
would allow you to have compressed archives of encrypted modules located
on a web server without any of the hooks knowing anything about each
other.

Cheers,
Brian