[Python-3000] Draft PEP: Module Initialization and finalization
"Martin v. Löwis"
martin at v.loewis.de
Wed Apr 12 09:30:38 CEST 2006
Tim Peters wrote:
> PythonLabs never found the bandwidth for this, as we all spent the
> next year trying to teach Barry how to use Unicode. I don't even
> remember what the thread above was about, but it would be great if the
> PEP contained enough relevant words that I could finally clean that
> message out of my inbox ;-)
Sure: I think many of these cleanup points overlap, in the sense that
cleanup should get invoked if the module object is discarded. I can
put that in the discussion of the PEP (and it's in the python-3000
archives meanwhile to remind me):
> - when the module object is deleted from sys.modules
This may not have immediate effect, since somebody might still holding
a reference to the module. A new import will create a new module object,
so I guess it would be best if indeed the state lives in the module
> - when reload() is used on an extension
This is trickier: I guess reload should invoke the finalize function,
discard the memory for the state, and then invoke the initialize
function again, which will allocate a new state block.
> - when an interpreter created with PyInterpreter_New() is finalized
This shouldn't cause any module finalization directly. Instead,
sys.modules of the interpreter gets cleared, which should cause
the module objects to be released.
> - when Py_Finalize() is called
Likewise. Of course, a forced clearing of all modules might be
desirable, since the modules might keep a cycle alive.
This raises the question whether modules should be able to
participate in cyclic GC...
> - when Python exits
That should not cause action: Py_Finalize should clear it all.
> - when the Python DLL is unloaded (Windows only)
IMO, the DLL should only get unloaded when all modules referring to it
have been unloaded. So, when the DLL is unloaded, no action should
be taken. I also think it's not Windows only: on dl, we should invoke
dlclose when all modules have been finalized.
More information about the Python-3000