
2009/10/16 Amaury Forgeot d'Arc <amauryfa@gmail.com>:
2009/10/16 Neil Schemenauer <nas@arctrix.com>:
After some experimentation I realize this idea is not ready yet. The main problem comes from references to Python objects that modules keep but don't expose to the garbage collector. For example, gcmodule.c has a static pointer "tmod" that is a reference to the "time" module. This reference prevents the "time" module from being freed during interpreter shutdown.
Ideally, I suppose modules should be treated like any other object and have tp_traverse and tp_clear methods that deal with these sorts of pointers. They would have to delegated to the instance since each module would have its own implementation.
Note since python 3.0 (and PEP 3121), the PyModuleDef structure has some members like m_traverse, m_clear and m_free for this very purpose. So far, nobody cared to implement these methods for any module. Maybe one should start at least for static PyObject* that contain references to modules.
I believe the implementation is buggy because modules (and their states) can easily be finalized before the objects contained in them. For example, when I tried to convert the _io module to use a state, it resulted in segfaults when the dealloc methods of objects tried to use objects in a state, which had already been deallocated. -- Regards, Benjamin