[Python-Dev] Fun with 2.3 shutdown

Phillip J. Eby pje at telecommunity.com
Tue Sep 23 12:09:18 EDT 2003

At 04:27 PM 9/23/03 +0100, Armin Rigo wrote:
>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".

If I understand your suggestion correctly, the following code would break 
under such an arrangement:

x = 1
def getX():
    return X

from Foo import getX  # returns getX() bound to one set of globals

import Foo   # imports a *new* Foo module, with new dictionary!
Foo.x = 2    # change the new Foo.x to 2

print getX()  # call the getX() with the old globals, and print 1

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.

More information about the Python-Dev mailing list