Author: brett.cannon
Date: Thu Feb 1 01:20:25 2007
New Revision: 53609
Modified:
python/branches/bcannon-objcap/BRANCHNEWS
python/branches/bcannon-objcap/Python/pythonrun.c
Log:
Get the 'warnings' module cached away in the C code so that the module is still
usable by C code.
Modified: python/branches/bcannon-objcap/BRANCHNEWS
==============================================================================
--- python/branches/bcannon-objcap/BRANCHNEWS (original)
+++ python/branches/bcannon-objcap/BRANCHNEWS Thu Feb 1 01:20:25 2007
@@ -8,6 +8,8 @@
Core and builtins
-----------------
+* Force 'warnings' to be cached by the C code.
+
* 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
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 01:20:25 2007
@@ -334,6 +334,7 @@
PyInterpreterState *interp;
Py_ssize_t module_count, x;
PyObject* module_names_list;
+ PyObject* hidden_modules;
Py_InitializeEx(1);
@@ -359,32 +360,53 @@
Safe to keep around.
* encodings
Does dynamic import of encodings which requires globals() to
- work; globals() fails when the module has been deleted.
- * encodings.utf_8
- Many encodings use this.
+ work; globals() fails when the module has been deleted. Also
+ fails if you hide module because importing of submodules for
+ encodings no longer has the parent package.
* codecs
Incremental codecs fail.
- * warnings
+ * _codecs
+ Exposed by codecs.
+ * warnings (hide: needs sys._getframe())
Warnings reset otherwise.
*/
+ /* Get the 'warnings' module cached away at the C level. */
+ PyModule_GetWarningsModule();
module_names_list = PyDict_Keys(interp->modules);
module_count = PyList_GET_SIZE(module_names_list);
+ hidden_modules = PyDict_New();
for (x=0; x < module_count; x+=1) {
char *module_name =
PyString_AS_STRING(
PyList_GET_ITEM(module_names_list, x));
- if ((strcmp(module_name, "__builtin__") != 0) &&
- (strcmp(module_name, "exceptions") != 0) &&
- (strcmp(module_name, "__main__") != 0) &&
- (strcmp(module_name, "encodings") != 0) &&
- (strcmp(module_name, "encodings.utf_8") != 0) &&
- (strcmp(module_name, "codecs") != 0) &&
- (strcmp(module_name, "warnings") != 0)) {
+ /* Modules that *must* stay visible. */
+ if ((strcmp(module_name, "__builtin__") == 0) ||
+ (strcmp(module_name, "__main__") == 0) ||
+ (strcmp(module_name, "exceptions") == 0) ||
+ (strcmp(module_name, "encodings") == 0) ||
+ (strcmp(module_name, "codecs") == 0) ||
+ (strcmp(module_name, "_codecs") == 0)) {
+ continue;
+ }
+ /* Modules that *must* stay but can be invisible. */
+ /*else if ((strcmp(module_name, "warnings") == 0)) {
+ PyObject *module =
+ PyDict_GetItemString(interp->modules,
+ module_name);
+ PyDict_SetItemString(hidden_modules, module_name,
+ module);
+ PyDict_DelItemString(interp->modules, module_name);
+ }*/
+ /* Everything else can go. */
+ else {
PyDict_DelItemString(interp->modules, module_name);
}
}
- PyDict_SetItemString(interp->sysdict, "modules", interp->modules);
+ /* Store away modules that must stick around but should not be exposed.
+ */
+ PyDict_SetItemString(interp->modules, ".hidden", hidden_modules);
+ PyDict_SetItemString(interp->sysdict, "modules", interp->modules);
}