[Python-Dev] New Import Hooks PEP, a first draft (and req. for PEP #)

Just van Rossum just@letterror.com
Fri, 20 Dec 2002 21:19:52 +0100


Thomas Heller wrote:

> Looks ok to me, although I would prefer to write
> 
>         path = os.sep.join(__name__.split(".") + ["wininst.exe"])
> or
>         path = os.path.join(*__name__.split(".") + ["wininst.exe"])

Much better. I think I'd prefer the first, mostly because os.path.join()
might do more magic than needed.

I think it's time to pin down the next extensions to the importer
protocol. How about the next speclet:

    bool = loader.is_package(fullname)
    code = loader.get_code(fullname)
    str = loader.get_source(fullname)

(is_package and get_code are needed for modulefinder-like tools,
get_source is needed for linecache-like tools.)

All three should raise ImportError if the module isn't available.
get_code and get_source should return None if there's no code or source
associated with the module, respectively. "No code" means it's a builtin
module or an extension. "No source" means just that (eg. for zipimport
it would mean "I've only got a .pyc or .pyo file for you").

If the importer does support get_source(), yet doesn't have a code
object readily available, get_code() should compile the source by itself
and return the code. This is to make life easier for the caller: if it
only needs the code object it shouldn't need to also check get_source()
and deal with that.

They should be optional, yet strongly recommended for general purpose
hooks. There's no need to support them for hooks that are specific to a
deliverable app; for example zipimport should have them, but if Thomas
writes a dedicated hook for the apps built by py2exe, he should be free
to leave them out.

Btw. would it be better if mod.__importer__ was named mod.__loader__?
This is closer to the truth, yet I fear that __loader__ is too generic.

> > PS: please take care to not Cc to peps@python.org any more in this
> > thread, I think I've done enough harm ;-)
> 
> Sorry for that, I noticed it as it was already too late.

No need to be sorry, it was my fault...

Just