[Python-Dev] New universal import mechanism ( Re: [Python-checkins] python/dist/src/Python import.c,2.210,2.211)

Jeff Epler jepler@unpythonic.net
Tue, 3 Dec 2002 08:24:20 -0600


On Tue, Dec 03, 2002 at 03:59:05AM -0500, Guido van Rossum wrote:
> Alas, the current import hooking mechanisms don't allow control over
> this (the interpretation of * is hardcoded).  Feel free to suggest an
> API and/or implementation of a hook for that, after reading how it's
> done now.  The crucial code is import_all_from() in ceval.c.

Currently __all__ is a list of all the attributes to be exported by the
module.  What if we let __all__ be a list (with its current
interpretation) or a callable.

If __all__ is callable, then it is called with two parameters: the
importing module, and the imported module.  It can perform the desired
operation.  Any returned value is ignored, any raised exception is
propagated.

In David Abraham's case, the hook might look something like this:

    def __all__(exporter, importer):
        for name, attr in vars(exporter).items():
            if name.startswith('_'): continue
            if isinstance(attr, Multimethod):
                merge_one_multimethod(importer, name, attr)
            else:
                setattr(importer, name, method)

(David, apologies if I've simplified this too much.  All I know about
your desired functionality is that you want to "merge multimethods", and
I have some idea what a multimethod is..)

Jeff