[Python-checkins] r51917 - in python/branches/bcannon-objcap: Lib/test/test_interpreter.py Modules/interpretermodule.c

brett.cannon python-checkins at python.org
Tue Sep 19 01:48:17 CEST 2006


Author: brett.cannon
Date: Tue Sep 19 01:48:16 2006
New Revision: 51917

Modified:
   python/branches/bcannon-objcap/Lib/test/test_interpreter.py
   python/branches/bcannon-objcap/Modules/interpretermodule.c
Log:
Tweak the setter for 'modules' so that it also assigns the new dict to
sys.modules.  Also strenthen tests for 'modules'.


Modified: python/branches/bcannon-objcap/Lib/test/test_interpreter.py
==============================================================================
--- python/branches/bcannon-objcap/Lib/test/test_interpreter.py	(original)
+++ python/branches/bcannon-objcap/Lib/test/test_interpreter.py	Tue Sep 19 01:48:16 2006
@@ -134,6 +134,15 @@
         del self.interp.modules['token']
         # XXX should really check that ImportError not raised.
         self.interp.execute('import token')
+        
+    def test_replacing(self):
+        # Replacing with a new dict should work.
+        new_modules = self.interp.modules.copy()
+        self.interp.modules = new_modules
+        _return = []
+        self.interp.builtins()['_return'] = _return
+        self.interp.execute('import sys; _return.append(id(sys.modules))')
+        self.failUnlessEqual(id(new_modules), _return[-1])
 
     def test_fresh(self):
         # Make sure that imported Python modules are new instances.
@@ -157,12 +166,11 @@
         self.failUnless('version' in sys_dict)
 
     def test_set(self):
-        # Make sure sys_dict can be set to a new dict and that  it has desired
+        # Make sure sys_dict can be set to a new dict and that it has desired
         # effect.
         sys_dict_copy = self.interp.sys_dict.copy()
         self.interp.sys_dict = sys_dict_copy
         self.failUnlessRaises(TypeError, setattr, self.interp, 'sys_dict', [])
-        # XXX requires exceptions.
 
     def test_mutating(self):
         # Changes to the dict should be reflected in the interpreter.
@@ -171,7 +179,7 @@
         interp_return = []
         self.interp.builtins()['to_return'] = interp_return
         self.interp.execute(test_sys_changed)
-        self.failUnless(interp_return[0])
+        self.failUnless(interp_return[-1])
 
     def test_deletion(self):
         # Make sure removing a value raises the proper exception when accessing

Modified: python/branches/bcannon-objcap/Modules/interpretermodule.c
==============================================================================
--- python/branches/bcannon-objcap/Modules/interpretermodule.c	(original)
+++ python/branches/bcannon-objcap/Modules/interpretermodule.c	Tue Sep 19 01:48:16 2006
@@ -137,6 +137,16 @@
 
 /*
    Setter for 'modules'.
+ 
+ Assumes no one has stored away a reference to sys.modules .  Since sys.modules
+ is set during interpreter creation, its reference is not updated unless done so
+ explicitly.
+ 
+ One option would have been to take the approach of builtins(), which is to have
+ a function that returns the initial dict and thus prevent complete dict
+ replacement.  But since assigning to sys.modules directly has not effect,
+ assuming people would treat sys.modules as something to not store away seemed
+ reasonable.
 */
 static int
 interpreter_set_modules(PyObject *self, PyObject *arg, void *optional)
@@ -152,6 +162,8 @@
 	Py_INCREF(arg);
 	Py_DECREF(old_modules);
 	PyInterpreter_GET_INTERP(self)->modules = arg;
+        PyDict_SetItemString(PyInterpreter_GET_INTERP(self)->sysdict,
+                             "modules", arg);
 
 	return 0;
 }


More information about the Python-checkins mailing list