[Python-Dev] PEP 302 and 'reload()'

Phillip J. Eby pje at telecommunity.com
Wed Sep 8 23:38:32 CEST 2004

It appears to me there is an error in both PEP 302's specification and its 
implementation concerning the correct operation of reload().  First, it says:

     The load_module() method has a few responsibilities that it must
     fulfill *before* it runs any code:

     - It must create the module object.  From Python this can be done
       via the new.module() function, the imp.new_module() function or
       via the module type object; from C with the PyModule_New()
       function or the PyImport_ModuleAdd() function.

This should probably say that if the module already exists in sys.modules, 
it should reuse the existing module object, rather than creating a new 
one.  Otherwise, 'reload()' cannot fulfill its contract.

Second, the actual implementation of PyImport_ReloadModule doesn't actually 
use a loader object, so reload() doesn't work with import hooks at 
all.  There's an SF bug report for this, and a patch to fix it (that also 
adds a test to test_importhooks to ensure that 'reload()' actually invokes 
the loader.

Are there any objections to me fixing either/both of these, and backporting 
the bugfix to the 2.3 maintenance branch?

Also, should PyImport_ReloadModule use the import lock?  It doesn't 
currently, but I'm not clear on why it doesn't.

More information about the Python-Dev mailing list