[Python-Dev] New and Improved Import Hooks

Moore, Paul Paul.Moore@atosorigin.com
Fri, 6 Dec 2002 09:38:50 -0000


From: Gordon McMillan [mailto:gmcm@hypernet.com]
> BTW, iu does allow non string objects in sys.path.
> If it exposes "getmod(name)", that will be tried
> in preference to iu's own machinery.

I just tried this out. As far as I can see, iu works perfectly
with a simple getmod() method. Why does your patch require
find_module() and load_module()?

I'm not criticising, just trying to understand the
requirements... (Anything to make writing an import hook
easier!!!)

You'll notice I'm wavering over my dislike of having objects on
sys.path :-)

My interest in import hooks stems from wanting to be able to
implement something like Java's 'executable jar file' support,
where an application, data and all, can be packaged up as a
single file. In theory, there's no real problem with this (I can
write import hooks to load from a zip file right now), but in
practice, the bootstrapping problems are quite nasty. The more
in-core (or at least standard library) support there is for these
features, the less of a bootstrap problem I have...

Paul.

"""
Example code for iu (adds an object to sys.path which satisfies *all*
requests for modules and packages with a new empty module...)
"""

import iu
import imp
import sys

iu.ImportManager().install()

class Tester:
    def getmod(self, name):
        print "Requested", name, locals()
        m =3D imp.new_module(name)
        # Either set __path__ to contain an object,
        # or set __path__ to contain a path, and
        # __importsub__ to the object...
        m.__path__ =3D [ Tester() ]
        sys.modules[name] =3D m
        return m

sys.path.append(Tester())