[Python-3000-checkins] r58429 - in python/branches/py3k-importlib: Include/pythonrun.h NEWS Python/import.c Python/pythonrun.c

brett.cannon python-3000-checkins at python.org
Fri Oct 12 09:20:04 CEST 2007


Author: brett.cannon
Date: Fri Oct 12 09:20:04 2007
New Revision: 58429

Modified:
   python/branches/py3k-importlib/Include/pythonrun.h
   python/branches/py3k-importlib/NEWS
   python/branches/py3k-importlib/Python/import.c
   python/branches/py3k-importlib/Python/pythonrun.c
Log:
Move over to using importlib for __import__.  Squirrel away original __import__
in __old_import__.  Had to create a dinky file class to handle the case where
site.py has not been imported yet and thus has not set __builtins__.open().
Debugging is hard since sys.stderr is not set if site.py cannot be successfully
imported, so one must be careful when changing the importlib code.

This is not thoroughly tested!  But test_import works sans the test requiring
warnings; that won't pass until warnings is a built-in module.


Modified: python/branches/py3k-importlib/Include/pythonrun.h
==============================================================================
--- python/branches/py3k-importlib/Include/pythonrun.h	(original)
+++ python/branches/py3k-importlib/Include/pythonrun.h	Fri Oct 12 09:20:04 2007
@@ -123,7 +123,7 @@
 PyAPI_FUNC(PyObject *) _PyBuiltin_Init(void);
 PyAPI_FUNC(PyObject *) _PySys_Init(void);
 PyAPI_FUNC(void) _PyImport_Init(void);
-PyAPI_FUNC(void) _PyImport_Importlib(void);
+PyAPI_FUNC(void) _PyImport_Importlib(PyObject *, PyObject *);
 PyAPI_FUNC(void) _PyExc_Init(void);
 PyAPI_FUNC(void) _PyImportHooks_Init(void);
 PyAPI_FUNC(int) _PyFrame_Init(void);

Modified: python/branches/py3k-importlib/NEWS
==============================================================================
--- python/branches/py3k-importlib/NEWS	(original)
+++ python/branches/py3k-importlib/NEWS	Fri Oct 12 09:20:04 2007
@@ -1,3 +1,5 @@
+* Store old __import__ into __old_import__ and use importlib.
+
 * Add _r_long() and _w_long() to marshal.
 
 * Add _case_ok() to imp.

Modified: python/branches/py3k-importlib/Python/import.c
==============================================================================
--- python/branches/py3k-importlib/Python/import.c	(original)
+++ python/branches/py3k-importlib/Python/import.c	Fri Oct 12 09:20:04 2007
@@ -525,7 +525,7 @@
 
 
 void
-_PyImport_Importlib(void)
+_PyImport_Importlib(PyObject *builtins, PyObject *modules)
 {
     const char *importlib_path = Py_GetImportlibPath();
     FILE *fp = NULL;
@@ -536,6 +536,7 @@
     unsigned int x;
     PyObject *modules_dict = NULL;
     PyObject *attr = NULL;
+    PyObject *obj = NULL;
 
 
     /* _importlib must have been found. */
@@ -556,22 +557,26 @@
 	    Py_FatalError("could not initialize _importlib");
 
     /* Get sys.modules so as to extract needed built-in modules. */
-    modules_dict = PyImport_GetModuleDict();
     for (x = 0; builtin_modules[x] != NULL; x += 1) {
 	    PyObject *module;
 	    const char *name = builtin_modules[x];
 
-	    /* Initialize the built-in module. */
-	    if (!init_builtin((char *)name))
-		    Py_FatalError("initialization of a built-in module failed");
-	    /* Get the module from sys.modules. */
-	    module = PyDict_GetItemString(modules_dict, name);
-	    if (!module)
+	    /* See if sys.modules already has the module to avoid a re-init. */
+	    module = PyDict_GetItemString(modules, name);
+	    if (module == NULL) {
+		    /* Initialize the built-in module. */
+		    if (!init_builtin((char *)name))
+			    Py_FatalError("initialization of a built-in "
+					    "module failed");
+		    /* Get the module from sys.modules. */
+		    module = PyDict_GetItemString(modules, name);
+	    }
+	    if (module == NULL)
 		    Py_FatalError("built-in module lost");
 	    /* Add the module to _importlib's globals. */
 	    Py_INCREF(module);
 	    if (PyModule_AddObject(importlib,
-				    (strcmp(name, PyOS_MODNAME) ? name : "_os"),
+				    (strcmp(name, PyOS_MODNAME) != 0 ? name : "_os"),
 				    module))
 		    Py_FatalError("could not add built-in module to _importlib");
     }
@@ -583,7 +588,25 @@
     if (PyModule_AddObject(importlib, "path_sep", attr) < 0)
 	    Py_FatalError("could not add path_sep to _importlib");
 
+    /* Store away old __import__. */
+    obj = PyDict_GetItemString(builtins, "__import__");
+    if (obj == NULL || PyErr_Occurred())
+	    Py_FatalError("error getting old __import__");
+    if (PyDict_SetItemString(builtins, "__old_import__", obj))
+	    Py_FatalError("unable to store away old __import__");
+
+    /* Create an instance of Import and set __import__ to it. */
+    modules_dict = PyModule_GetDict(importlib);
+    attr = PyDict_GetItemString(modules_dict, "Import");
+    if (attr == NULL || PyErr_Occurred())
+	    Py_FatalError("couldn't get _importlib.Import");
+    obj = PyObject_CallObject(attr, NULL);
+    if (obj == NULL)
+	    Py_FatalError("unable to instantiate _importlib.Import");
+    if (PyDict_SetItemString(builtins, "__import__", obj))
+		    Py_FatalError("could not set new __import__");
 
+    Py_DECREF(obj);
     Py_DECREF(importlib);
 }
 

Modified: python/branches/py3k-importlib/Python/pythonrun.c
==============================================================================
--- python/branches/py3k-importlib/Python/pythonrun.c	(original)
+++ python/branches/py3k-importlib/Python/pythonrun.c	Fri Oct 12 09:20:04 2007
@@ -237,12 +237,12 @@
 
 	_PyImportHooks_Init();
 
-	_PyImport_Importlib();
-
 	if (install_sigs)
 		initsigs(); /* Signal handling stuff, including initintr() */
 
 	initmain(); /* Module __main__ */
+
+	_PyImport_Importlib(interp->builtins, interp->modules);
 	if (!Py_NoSiteFlag)
 		initsite(); /* Module site */
 


More information about the Python-3000-checkins mailing list