[Python-Dev] Fun with 2.3 shutdown

Armin Rigo arigo at tunes.org
Tue Sep 23 11:27:56 EDT 2003


Hello Jim,

On Sun, Sep 21, 2003 at 06:06:45AM -0400, Jim Fulton wrote:
> Or, given cyclic GC, why isn't it enough to clear sys.modules?

Because the modules dictionary is stored in
PyThreadState_Get()->interp->modules, where the GC cannot see it, which
prevent the dictionary from being freed; in turn, it prevents any module from
being freed.

Still, it means (at a first sight) that there is actually no need to clear
each module's globals at all. Just removing them all from interp->modules is
fine. In practice there are probably a few issues that I'm not fully aware of,
like what occurs if we change module_dealloc() so that it no longer clears the
module's global dictionary -- a tweak that seems to come from a pre-GC
history.

Not speaking about threads in particular, and compatibility problems apart,
wouldn't it make sense to change sys.modules and interp->modules into a
WeakValueDictionary (or some equivalent refcount-based hack like the one for
interned strings)? This would give memory management at the module level, and
would allow a cleaner shutdown procedure as well. It would however change
Python's semantics of "a module is only loaded once unless you ask or you mess
with sys.modules explicitely".


A bientot,

Armin.




More information about the Python-Dev mailing list