[Python-Dev] import.c

Phillip J. Eby pje at telecommunity.com
Fri May 28 18:20:05 EDT 2004


At 04:32 PM 5/28/04 -0400, Tim Peters wrote:
>Does anyone living understand import.c in all its gory glory now?
>
>There are two things about it I'd like to address (or see addressed):
>
>1. The business wherein a failing import leaves behind a (at least
>    one) damaged (incomplete) module object in sys.modules has caused
>    me debugging nightmares for years.  These are especially acute
>    when "clever" code suppresses the errors.  Then sys.modules
>    can claim to have modules that aren't actually usable, but
>    would-be importers have no way to know which are damaged, or
>    how badly damaged they may be.
>
>2. Zope3 in particular is having horrid problems with circular
>    imports.  These are also a bitch to track down.  I'm not sure
>    it was any better in Zope2, but Zope3 has been undergoing
>    frequent and massive refactorings (and a new round of unintended
>    circular imports creates subtle problems each time).
>
>If someone wants to fix these over the weekend, that would be great <wink>.

I don't know if I'd call this a "fix", exactly, but PEAK has a "lazy 
module" mechanism that can address some of these issues, and might be 
extended to paper over them a bit more.

Specifically, take a look at:

http://cvs.eby-sarna.com/PEAK/src/peak/util/imports.py?rev=1.16&content-type=text/vnd.viewcvs-markup

from the 'lazyModule()' function down.  Given an (absolute) module name, 
this function returns a module (subclass) object that is:

1) in sys.modules
2) is not yet imported
3) will be imported as soon as its __getattribute__ method is invoked
4) will cancel its importedness (albeit without clearing the module 
dictionary) if an exception occurs during the import

Of course, it does this by relying on various semidocumented properties of 
the current import system implementation, like how 'reload()' works, so if 
you were looking to *simplify* things, this might be the wrong place to 
look.  :)




More information about the Python-Dev mailing list