[Python-Dev] Write All New Import Hooks (PEP 302) in Python, Not C

Just van Rossum just@letterror.com
Sat, 28 Dec 2002 18:17:21 +0100


Michael Hudson wrote:

> I've not really been following this discussion with utmost care, but
> the name "meta_path" doesn't move me.  Wouldn't sys.importers be
> better name?  Or maybe I'm misunderstanding it's intent -- which is
> still an argument for a better name.

I don't like the name much either, but it _does_ fit quite well as the
idea is that sys.path importing gets invoked through an item on
meta_path. Perhaps this sketch helps a little:


sys.meta_path = [
    BuiltinImporter(),  # handles builtin modules
    FrozenImporter(),   # handles frozen modules
    PathImporter(),     # handles sys.path
]


def find_module(fullname, path=None):
    for i in sys.meta_path:
        l = i.find_module(fullname, path)
        if l is not None:
            return l
    return None


class PathImporter:

    def find_module(self, fullname, path=None):
        if path is None:
            path = sys.path
        for p in path:
            i = get_path_importer(p) # deals with sys.path_hooks & cache
            if i is not None:
                l = i.find_module(fullname)
                if l is not None:
                    return l
        retun None


This is how you should look at it, even though it doesn't match the
implementation (yet). Right now you should imagine those three objects
on meta_path as an implicit extension of sys.meta_path, they don't yet
physically exist. This should change in the near future, perhaps even
with 2.3a2.

Just