[Python-checkins] r53391 - python/branches/bcannon-objcap/Python/pythonrun.c python/branches/bcannon-objcap/Python/sysmodule.c

brett.cannon python-checkins at python.org
Thu Jan 11 22:34:16 CET 2007


Author: brett.cannon
Date: Thu Jan 11 22:34:15 2007
New Revision: 53391

Modified:
   python/branches/bcannon-objcap/Python/pythonrun.c
   python/branches/bcannon-objcap/Python/sysmodule.c
Log:
Add an import delegate to the sys module that just calls the object stored in
sys.import_.  This should allow a Python implementation of import to be used
safely without exposing dangerous things (e.g., func_globals when the Python
code imports things that should be be made available to other code).


Modified: python/branches/bcannon-objcap/Python/pythonrun.c
==============================================================================
--- python/branches/bcannon-objcap/Python/pythonrun.c	(original)
+++ python/branches/bcannon-objcap/Python/pythonrun.c	Thu Jan 11 22:34:15 2007
@@ -250,6 +250,9 @@
 	   needed. */
 	PyDict_SetItemString(interp->sysdict, "import_",
 			PyDict_GetItemString(interp->builtins, "__import__"));
+	PyDict_SetItemString(interp->builtins, "__import__",
+			PyDict_GetItemString(interp->sysdict,
+				"import_delegate"));
 
 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
 	/* On Unix, set the file system encoding according to the

Modified: python/branches/bcannon-objcap/Python/sysmodule.c
==============================================================================
--- python/branches/bcannon-objcap/Python/sysmodule.c	(original)
+++ python/branches/bcannon-objcap/Python/sysmodule.c	Thu Jan 11 22:34:15 2007
@@ -733,6 +733,19 @@
 }
 #endif
 
+static PyObject *
+sys_import_delegate(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+	PyObject *import_object = PySys_GetObject("import_");
+
+	if (import_object == NULL) {
+		PyErr_SetString(PyExc_RuntimeError, "sys.import_ not set");
+		return NULL;
+	}
+
+	return PyObject_Call(import_object, args, kwargs);
+}
+
 static PyMethodDef sys_methods[] = {
 	/* Might as well keep this in alphabetic order */
 	{"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
@@ -776,6 +789,8 @@
 	{"getwindowsversion", (PyCFunction)sys_getwindowsversion, METH_NOARGS,
 	 getwindowsversion_doc},
 #endif /* MS_WINDOWS */
+	{"import_delegate", (PyCFunction)sys_import_delegate,
+		METH_VARARGS | METH_KEYWORDS, "XXX"},
 #ifdef USE_MALLOPT
 	{"mdebug",	sys_mdebug, METH_VARARGS},
 #endif


More information about the Python-checkins mailing list