Interpreter: Python 3.1.2 Sample: ===== first.py ===== import sys import second if 'second' in sys.modules: print ('in sys modules') del sys.modules['second'] del second ===== second.py ===== class A: def __init__(self): print('created') def __del__(self): print('destroyed') a = A() --------------------------------------------- Result: 'destroyed' isn't printed With Python 2.6.5 it worked fine
Probably , which will disable clearing of the module dict if it's caught in a reference cycle (which it is here, since A.__init__ and A.__del__ will reference it as their globals dict).
Tricky. I think the only way to do this properly is to call _PyModule_Clear when the dict is destroyed. However, there's no good way to flag a dictionary as a "module" dict. Therefore, I propose we remove the Py_REFCNT == 1 guard in module_dealloc, and simply leave as an open bug that modules will clear their dictionaries on deallocation. Thoughts?
> Therefore, I propose we remove the Py_REFCNT == 1 guard in > module_dealloc, and simply leave as an open bug that modules will clear > their dictionaries on deallocation. Yes, I think it's the best solution. People can easily copy the dict if they want to keep it around after the module gets destroyed.