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

brett.cannon python-checkins at python.org
Thu Sep 21 01:56:57 CEST 2006


Author: brett.cannon
Date: Thu Sep 21 01:56:57 2006
New Revision: 51944
Modified:
 python/branches/bcannon-objcap/Lib/test/test_interpreter.py
 python/branches/bcannon-objcap/Modules/interpretermodule.c
Log:
Make sys_dict a function since the data dict seems to get cached somewhere.
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	Thu Sep 21 01:56:57 2006
@@ -134,7 +134,6 @@
 self.failUnless('token' not in self.interp.modules)
 self.interp.execute('import token')
 del self.interp.modules['token']
- # XXX should really check that ImportError not raised.
 self.interp.execute('import token')
 
 def test_replacing(self):
@@ -155,7 +154,18 @@
 self.interp.execute("import token;"
 "_return.append(hasattr(token, 'test'))")
 self.failUnless(not _return[-1])
-
+ 
+ def test_not_cached(self):
+ # Make sure that 'modules' dict is not cached.
+ builtin = self.interp.modules['__builtin__']
+ main = self.interp.modules['__main__']
+ self.interp.execute("import token")
+ self.interp.modules = {}
+ self.interp.modules['__builtin__'] = builtin
+ self.interp.modules['__main__'] = main
+ self.interp.execute("import token")
+ self.failUnless('token' in self.interp.modules)
+ 
 
 class SysDictTests(BaseInterpTests):
 
@@ -163,20 +173,13 @@
 
 def test_get(self):
 # Make sure a dict is returned.
- sys_dict = self.interp.sys_dict
+ sys_dict = self.interp.sys_dict()
 self.failUnless(isinstance(sys_dict, dict))
 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
- # effect.
- self.failUnlessRaises(TypeError, setattr, self.interp, 'sys_dict', [])
- # XXX requires exceptions
- # set to new dict, make sure exceptions raised when trying to get attribute from sys
-
 def test_mutating(self):
 # Changes to the dict should be reflected in the interpreter.
- sys_dict = self.interp.sys_dict
+ sys_dict = self.interp.sys_dict()
 sys_dict['version'] = 'test'
 interp_return = []
 self.interp.builtins()['to_return'] = interp_return
@@ -186,7 +189,7 @@
 def test_deletion(self):
 # Make sure removing a value raises the proper exception when accessing
 # through the 'sys' module.
- del self.interp.sys_dict['version']
+ del self.interp.sys_dict()['version']
 # XXX requires exceptions
 # XXX self.failUnlessRaises(XXX, self.interp.execute,
 # 'import sys; sys.version')
@@ -194,14 +197,14 @@
 def test_copied(self):
 # sys_dict should be unique per interpreter (including mutable data
 # structures).
- sys_dict = self.interp.sys_dict
- sys_dict['version'] = 'test'
- self.failUnless(sys.version != 'test')
- # XXX check mutable data structures
- sys_dict.setdefault('argv', []).append('test')
- self.failUnless(sys.argv[-1] != 'test')
- sys_dict['path'].append('test')
- self.failUnless(sys.path[-1] != 'test')
+ sys_version = sys.version
+ self.interp.sys_dict()['version'] = 'test'
+ reload(sys)
+ self.failUnlessEqual(sys.version, sys_version)
+ _return = []
+ self.interp.builtins()['_return'] = _return
+ self.interp.execute("import sys; _return.append(sys.version)")
+ self.failUnlessEqual(_return[-1], 'test')
 
 
 class InputOutputTests(BaseInterpTests):
Modified: python/branches/bcannon-objcap/Modules/interpretermodule.c
==============================================================================
--- python/branches/bcannon-objcap/Modules/interpretermodule.c	(original)
+++ python/branches/bcannon-objcap/Modules/interpretermodule.c	Thu Sep 21 01:56:57 2006
@@ -64,6 +64,34 @@
 return (PyObject *)self;
 }
 
+/*
+ Getter for 'builtins'.
+ 
+ There is not setter because the creation of a new interpreter automatically
+ creates the initial execution frame which caches the built-in namespace.
+ */
+static PyObject *
+interpreter_builtins(PyObject *self)
+{
+ PyObject *builtins = PyInterpreter_GET_INTERP(self)->builtins;
+ 
+ Py_INCREF(builtins);
+ return builtins;
+}
+
+/*
+ Getter for 'sys_dict'.
+ 
+ There is no setter because the dict gets cached somewhere.
+ */
+static PyObject *
+interpreter_sys_dict(PyObject *self)
+{
+ PyObject *sys_dict = PyInterpreter_GET_INTERP(self)->sysdict;
+ 
+ Py_INCREF(sys_dict);
+ return sys_dict;
+}
 
 /*
 Execute Python source code in the interpreter.
@@ -103,21 +131,6 @@
 Py_RETURN_NONE;
 }
 
-/*
- Getter for 'builtins'.
- 
- There is not setter because the creation of a new interpreter automatically
- creates the initial execution frame which caches the built-in namespace.
- */
-static PyObject *
-interpreter_builtins(PyObject *self)
-{
- PyObject *builtins = PyInterpreter_GET_INTERP(self)->builtins;
- 
- Py_INCREF(builtins);
- return builtins;
-}
-
 static PyObject *
 redirect_output(PyObject *self, PyObject *args)
 {
@@ -164,6 +177,8 @@
 static PyMethodDef interpreter_methods[] = {
 {"builtins", (PyCFunction)interpreter_builtins, METH_NOARGS,
 "Return the built-in namespace dict."},
+ {"sys_dict", (PyCFunction)interpreter_sys_dict, METH_NOARGS,
+ "Return the 'sys' module's data dictionary."},
 {"execute", interpreter_exec, METH_O,
 	"Execute the passed-in string in the interpreter."},
 {"redirect_output", (PyCFunction)redirect_output, METH_VARARGS,
@@ -217,43 +232,8 @@
 	return 0;
 }
 
-/*
- Getter for 'sys_dict'.
-*/
-static PyObject *
-interpreter_get_sys_dict(PyObject *self, void *optional)
-{
-	PyObject *sys_dict = PyInterpreter_GET_INTERP(self)->sysdict;
-
-	Py_INCREF(sys_dict);
-	return sys_dict;
-}
-
-/*
- Setter for 'sys_dict'.
-*/
-static int
-interpreter_set_sys_dict(PyObject *self, PyObject *arg, void *optional)
-{
-	PyObject *old_sys_dict = PyInterpreter_GET_INTERP(self)->sysdict;
-
-	if (!PyDict_Check(arg)) {
-		PyErr_SetString(PyExc_TypeError,
-				"'sys_dict' must be set to a dict");
-		return -1;
-	}
-
-	Py_INCREF(arg);
-	Py_DECREF(old_sys_dict);
-	PyInterpreter_GET_INTERP(self)->sysdict = arg;
-
-	return 0;
-}
-
 
 static PyGetSetDef interpreter_getset[] = {
-	{"sys_dict", interpreter_get_sys_dict, interpreter_set_sys_dict,
-		"The modules dict for 'sys'.", NULL},
 	{"modules", interpreter_get_modules, interpreter_set_modules,
 		"The dict used for sys.modules.", NULL},
 	{NULL}


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /