[Python-3000] Interest in PEP for callbacks on module import

Phillip J. Eby pje at telecommunity.com
Sat Dec 8 22:04:55 CET 2007


At 09:55 PM 12/8/2007 +0100, Christian Heimes wrote:
>Nick Coghlan wrote:
> > I'm far from convinced that a from-scratch rewrite (particularly in C)
> > is a great idea myself - that's why I suggested a PEP to look at some of
> > the issues.
>
>Yesterday I wrote down a quick survey of codes with some use cases. Then
>I compared my ideas with PJE's import tool. His working implementation
>is almost identical to my idea. In my opinion we can copy his import
>callback API and re-implement it in C.
>
> > Some curly questions I thought of myself:
> >
> > - What do we do if something is inserted in sys.modules directly, and
> > then imported later? Do registered callbacks for that module trigger at
> > all? If so, when?
>
>I'll answer the question with a list of use cases
>
>* a new callback is registered and module is not loaded
>   o callback is added to a registry: module name -> list of callbacks
>
>* module is inserted into sys.modules[] manually
>   o nothing happens

Note that in my implementation, an entry is added to sys.modules 
immediately, so this scenario can't happen without *replacing* the 
sys.modules entry.


>* module is loaded with __import__() but callbacks weren't fired yet
>   o the existing import mechanism is called
>   o after the module is loaded and inserted into sys.modules
>     the callbacks are fired in reverse order (FILO).

Why not FIFO?


>What's the difference between lazy and weak importing?

"Weak importing" is my name for having a run-this-on-import callback, 
implemented in terms of lazy importing.

Lazy importing is putting a module object into sys.modules, but not 
loading its contents until you attempt to get or set a module attribute.



More information about the Python-3000 mailing list