[Python-Dev] is sys.modules not meant to be replaced? (original) (raw)

Benjamin Peterson benjamin at python.org
Sun Jul 24 07:09:13 CEST 2011


2011/7/24 Eric Snow <ericsnowcurrently at gmail.com>:

On Sat, Jul 23, 2011 at 10:55 PM, Benjamin Peterson <benjamin at python.org> wrote:

2011/7/23 Eric Snow <ericsnowcurrently at gmail.com>:

The documentation[1] doesn't say, but the implementation of the imp module makes me wonder if sys.modules was not meant to be replaceable.  No doubt this has to do with its tie to the interpreter's modules dict.  I ran into this doing "sys.modules = sys.modules.copy()" to protect the actual sys.modules dict during some import related test cases.  If the modules I imported were extension modules it broke.

So, is sys.modules not meant to be open to re-binding? Not any more or less than other global mutable objects. You can expect other code to be holding on to old references. But, isn't sys.modules a little different because other modules (at least the imp module) don't use it.  From what I understand the interpreter object's modules dict (to which sys.modules is initially bound) is used directly instead.  So rebinding sys.modules causes a disconnect.  That's why I am wondering if sys.modules is not meant to be rebound.

Sure. I'm not sure what point you're trying to make, though.

Are there other objects in the interpreter state that are exposed in sys that would have the same problem?

Is it problematic?

-- Regards, Benjamin



More information about the Python-Dev mailing list