[Python-Dev] Fun with 2.3 shutdown

Phillip J. Eby pje at telecommunity.com
Tue Sep 23 13:36:10 EDT 2003

At 05:49 PM 9/23/03 +0100, Armin Rigo wrote:
>Hello Phillip,
>On Tue, Sep 23, 2003 at 12:09:18PM -0400, Phillip J. Eby wrote:
> > So, unless a module's dictionary were to reference the module (or 
> functions
> > were to reference the module rather than (or in addition to) the module
> > dictionary), it seems the proposed semantics would lead to unexpected
> > results.
>I'm not sure I understand the reasons behind the current module/globals
>relationship.As modules zap their globals with None when they are

Well, a function can have globals that aren't a module dictionary, so 
that's probably why functions don't refer to their modules directly.  As 
for the None thing, it's to break circular references.

>deallocated, we observe the following behavior:
>def g():
>     return 5
>def f():
>     return g()
> >>> from foo import f
> >>> import sys; del sys.modules['test4']
> >>> f()
>Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "foo.py", line 4, in f
>     return g()
>TypeError: 'NoneType' object is not callable

Yeah, I've run into this (and weirder!) things before, when tracing down 
refcount bugs in C extensions.

But, I don't think anybody in their right mind would *rely* on this 
behavior, so your suggestion of e.g. clearing sys.modules and letting GC do 
the rest seems worthy of investigation.

>Possibly far-fetched, but I wouldn't be surprized to find large applications
>that mess with sys.modules in some way. For example, in one case, to ensure
>that a whole collection of interdependent modules will be reloaded on demand
>after I detect a change in one of them, I'm simply removing them all from

Of course, you have to also delete all modules and objects that reference 
the modules you want to delete.  And that would still be the case under 
your "clear sys.modules at shutdown" proposal.

OTOH, it's not clear what to do with uncollectable objects.  E.g., if you 
have a module global that is or contains an object with a __del__ method, 
and is part of a cycle back to that module.  I think you'd *still* be stuck 
going back and clearing the modules or setting their contents to None.

More information about the Python-Dev mailing list