[Python-checkins] r53608 - in python/branches/bcannon-objcap: BRANCHNEWS Include/moduleobject.h Objects/moduleobject.c Python/import.c Python/pythonrun.c

brett.cannon python-checkins at python.org
Thu Feb 1 00:31:46 CET 2007


Author: brett.cannon
Date: Thu Feb  1 00:31:44 2007
New Revision: 53608

Modified:
   python/branches/bcannon-objcap/BRANCHNEWS
   python/branches/bcannon-objcap/Include/moduleobject.h
   python/branches/bcannon-objcap/Objects/moduleobject.c
   python/branches/bcannon-objcap/Python/import.c
   python/branches/bcannon-objcap/Python/pythonrun.c
Log:
Change the import machinery on how it handles importing 'sys'.  Before the
import code used the cached dictionary that was created after _PySys_Init() was
called but before any subsequent items were added (e.g., sys.path).

Now, PyImport_FindExtension() (which is what uses the extension module cache
for importing new modules) checks if it is dealing with 'sys'.  If it is it
then sets the module's dict to the interpreter's sysdict instead of the cached
version.


Modified: python/branches/bcannon-objcap/BRANCHNEWS
==============================================================================
--- python/branches/bcannon-objcap/BRANCHNEWS	(original)
+++ python/branches/bcannon-objcap/BRANCHNEWS	Thu Feb  1 00:31:44 2007
@@ -8,6 +8,11 @@
 Core and builtins
 -----------------
 
+* Make importing the sys module after it has been completely deleted use the
+  interpreter's sysdict instead of the cached dict used by the import
+  machinery.  This lets the sys module be deleted for security reasons during
+  startup.
+
 * rev. ????: Added a delegate for import that calls sys.import_.
 
 * rev. 51679: Remove the constructor for the 'code' type.  This means instances

Modified: python/branches/bcannon-objcap/Include/moduleobject.h
==============================================================================
--- python/branches/bcannon-objcap/Include/moduleobject.h	(original)
+++ python/branches/bcannon-objcap/Include/moduleobject.h	Thu Feb  1 00:31:44 2007
@@ -14,6 +14,7 @@
 
 PyAPI_FUNC(PyObject *) PyModule_New(const char *);
 PyAPI_FUNC(PyObject *) PyModule_GetDict(PyObject *);
+PyAPI_FUNC(void) PyModule_SetDict(PyObject *, PyObject *);
 PyAPI_FUNC(char *) PyModule_GetName(PyObject *);
 PyAPI_FUNC(char *) PyModule_GetFilename(PyObject *);
 PyAPI_FUNC(void) _PyModule_Clear(PyObject *);

Modified: python/branches/bcannon-objcap/Objects/moduleobject.c
==============================================================================
--- python/branches/bcannon-objcap/Objects/moduleobject.c	(original)
+++ python/branches/bcannon-objcap/Objects/moduleobject.c	Thu Feb  1 00:31:44 2007
@@ -54,6 +54,16 @@
 	return d;
 }
 
+void
+PyModule_SetDict(PyObject *m, PyObject *new_dict)
+{
+	PyModuleObject *module = (PyModuleObject *)m;
+	PyObject *old_dict = module->md_dict;
+
+	Py_XDECREF(old_dict);
+	module->md_dict = new_dict;
+}
+
 char *
 PyModule_GetName(PyObject *m)
 {

Modified: python/branches/bcannon-objcap/Python/import.c
==============================================================================
--- python/branches/bcannon-objcap/Python/import.c	(original)
+++ python/branches/bcannon-objcap/Python/import.c	Thu Feb  1 00:31:44 2007
@@ -563,8 +563,17 @@
 	mdict = PyModule_GetDict(mod);
 	if (mdict == NULL)
 		return NULL;
-	if (PyDict_Update(mdict, dict))
-		return NULL;
+	if ((!strcmp("sys", name)) && (!strcmp("sys", filename))) {
+		PyThreadState *tstate = PyThreadState_GET();
+		PyObject *sysdict = tstate->interp->sysdict;
+
+		Py_INCREF(sysdict);
+		PyModule_SetDict(mod, sysdict);
+	}
+	else {
+		if (PyDict_Update(mdict, dict))
+			return NULL;
+	}
 	if (Py_VerboseFlag)
 		PySys_WriteStderr("import %s # previously loaded (%s)\n",
 			name, filename);

Modified: python/branches/bcannon-objcap/Python/pythonrun.c
==============================================================================
--- python/branches/bcannon-objcap/Python/pythonrun.c	(original)
+++ python/branches/bcannon-objcap/Python/pythonrun.c	Thu Feb  1 00:31:44 2007
@@ -357,9 +357,6 @@
 	       Current scope of execution.
 	   * exceptions
 	       Safe to keep around.
-	   * sys
-	       Certain values set during Python initialization that are lost
-	       when the module is deleted and then re-imported.
 	   * encodings
 	       Does dynamic import of encodings which requires globals() to
 	       work; globals() fails when the module has been deleted.
@@ -379,7 +376,6 @@
 		if ((strcmp(module_name, "__builtin__") != 0) &&
 			(strcmp(module_name, "exceptions") != 0) &&
 			(strcmp(module_name, "__main__") != 0) &&
-			(strcmp(module_name, "sys") != 0) &&
 			(strcmp(module_name, "encodings") != 0) &&
 			(strcmp(module_name, "encodings.utf_8") != 0) &&
 			(strcmp(module_name, "codecs") != 0) &&


More information about the Python-checkins mailing list