[Python-Dev] test___all__ polluting sys.modules?

Richard Oudkerk shibturn at gmail.com
Sun Dec 30 13:43:10 CET 2012

On 30/12/2012 12:31am, Eli Bendersky wrote:
> Would it make sense to save the sys.modules state and restore it in
> test___all__ so that sys.modules isn't affected by this test?

Deleting module objects can cause problems because the destructor 
replaces values in the globals dict by None.  If anything defined there 
has "escaped" and depends on any globals then you are liable to 
encounter errors.

For example, setuptools restores sys.modules after running each test. 
This was causing errors at shutdown from an atexit function registered 
by multiprocessing.  The atexit function was still registered, but no 
longer valid, because the module had been garbage collected and the 
globals had been replaced by None.

Personally I would like to get rid of the "purge globals" behaviour for 
modules deleted before shutdown has started: if someone manipulates 
sys.modules then they can just call gc.collect() if they want to 
promptly get rid of orphaned reference cycles.


More information about the Python-Dev mailing list