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

brett.cannon python-3000-checkins at python.org
Sat Sep 22 23:32:46 CEST 2007


Author: brett.cannon
Date: Sat Sep 22 23:32:45 2007
New Revision: 58233

Modified:
   python/branches/py3k-importlib/Include/pythonrun.h
   python/branches/py3k-importlib/Modules/posixmodule.c
   python/branches/py3k-importlib/NEWS
   python/branches/py3k-importlib/Python/import.c
Log:
Inject needed modules (sans warnings as it is not a built-in module yet) into
_importlib.  Still missing required attributes and exposure of certain C
functions (see importlib for what is needed).


Modified: python/branches/py3k-importlib/Include/pythonrun.h
==============================================================================
--- python/branches/py3k-importlib/Include/pythonrun.h	(original)
+++ python/branches/py3k-importlib/Include/pythonrun.h	Sat Sep 22 23:32:45 2007
@@ -152,6 +152,21 @@
 PyAPI_DATA(char) *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
 PyAPI_DATA(PyThreadState*) _PyOS_ReadlineTState;
 
+/* Name and init function for the platform-dependent os module */
+#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
+#define PyOS_INITFUNC initnt
+#define PyOS_MODNAME "nt"
+
+#elif defined(PYOS_OS2)
+#define PyOS_INITFUNC initos2
+#define PyOS_MODNAME "os2"
+
+#else
+#define PyOS_INITFUNC initposix
+#define PyOS_MODNAME "posix"
+#endif
+
+
 /* Stack size, in "pointers" (so we get extra safety margins
    on 64-bit platforms).  On a 32-bit platform, this translates
    to a 8k margin. */

Modified: python/branches/py3k-importlib/Modules/posixmodule.c
==============================================================================
--- python/branches/py3k-importlib/Modules/posixmodule.c	(original)
+++ python/branches/py3k-importlib/Modules/posixmodule.c	Sat Sep 22 23:32:45 2007
@@ -7239,25 +7239,12 @@
 }
 
 
-#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
-#define INITFUNC initnt
-#define MODNAME "nt"
-
-#elif defined(PYOS_OS2)
-#define INITFUNC initos2
-#define MODNAME "os2"
-
-#else
-#define INITFUNC initposix
-#define MODNAME "posix"
-#endif
-
 PyMODINIT_FUNC
-INITFUNC(void)
+PyOS_INITFUNC(void)
 {
 	PyObject *m, *v;
 
-	m = Py_InitModule3(MODNAME,
+	m = Py_InitModule3(PyOS_MODNAME,
 			   posix_methods,
 			   posix__doc__);
 	if (m == NULL)
@@ -7285,7 +7272,7 @@
 #endif
 
 	if (!initialized) {
-		stat_result_desc.name = MODNAME ".stat_result";
+		stat_result_desc.name = PyOS_MODNAME ".stat_result";
 		stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
 		stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
 		stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
@@ -7293,7 +7280,7 @@
 		structseq_new = StatResultType.tp_new;
 		StatResultType.tp_new = statresult_new;
 
-		statvfs_result_desc.name = MODNAME ".statvfs_result";
+		statvfs_result_desc.name = PyOS_MODNAME ".statvfs_result";
 		PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
 	}
 	Py_INCREF((PyObject*) &StatResultType);

Modified: python/branches/py3k-importlib/NEWS
==============================================================================
--- python/branches/py3k-importlib/NEWS	(original)
+++ python/branches/py3k-importlib/NEWS	Sat Sep 22 23:32:45 2007
@@ -1,4 +1,6 @@
-* Add _PyImport_Importlib() to import _importlib.
+* Add _PyImport_Importlib() to import _importlib.  This also involves importing
+  sys, imp, marshal and _os (which can be either nt, os2, or posix; reason why
+  PyOS_MODNAME and PyOS_INITFUNC have been added).
 
 * Add Py_GetImportlibPath() as a way to get the path to _importlib.
 

Modified: python/branches/py3k-importlib/Python/import.c
==============================================================================
--- python/branches/py3k-importlib/Python/import.c	(original)
+++ python/branches/py3k-importlib/Python/import.c	Sat Sep 22 23:32:45 2007
@@ -107,6 +107,8 @@
 /* Forward declarations */
 static PyTypeObject NullImporterType;
 static PyCodeObject * parse_source_module(const char *, FILE *);
+static int init_builtin(char *);
+
 
 /* Initialize things */
 
@@ -528,12 +530,23 @@
     const char *importlib_path = Py_GetImportlibPath();
     FILE *fp = NULL;
     PyCodeObject *code_object = NULL;
-    PyObject *module = NULL;
+    PyObject *importlib = NULL;
+    PyObject *modules= NULL;
+    PyObject *builtin_module = NULL;
 
 
     if (importlib_path[0] == '\0')
         Py_FatalError("_importlib.py not found");
 
+    if (!init_builtin("sys"))
+	    Py_FatalError("initializiation of sys failed");
+    if (!init_builtin("imp"))
+	    Py_FatalError("initialization of imp failed");
+    if (!init_builtin("marshal"))
+	    Py_FatalError("initialization of marshal failed");
+    if (!init_builtin(PyOS_MODNAME))
+	    Py_FatalError("initializatino of _os failed");
+
     fp = fopen(importlib_path, "r");
     code_object = parse_source_module(importlib_path, fp);
     fclose(fp);
@@ -541,12 +554,43 @@
     if (!code_object)
 	    Py_FatalError("unable to parse _importlib");
 
-    module = PyImport_ExecCodeModuleEx("_importlib", (PyObject *)code_object,
+    importlib = PyImport_ExecCodeModuleEx("_importlib", (PyObject *)code_object,
 					    (char *)importlib_path);
-    if (!module)
+    if (!importlib)
 	    Py_FatalError("could not initialize _importlib");
 
-    Py_DECREF(module);
+    modules = PyImport_GetModuleDict();
+
+    builtin_module = PyDict_GetItemString(modules, "sys");
+    if (!builtin_module)
+	    Py_FatalError("sys module lost");
+    Py_INCREF(builtin_module);
+    if (PyModule_AddObject(importlib, "sys", builtin_module) < 0)
+	    Py_FatalError("could not add sys to _importlib");
+
+    builtin_module = PyDict_GetItemString(modules, "imp");
+    if (!builtin_module)
+	    Py_FatalError("imp module lost");
+    Py_INCREF(builtin_module);
+    if (PyModule_AddObject(importlib, "imp", builtin_module) < 0)
+	    Py_FatalError("could not add imp to _importlib");
+
+    builtin_module = PyDict_GetItemString(modules, "marshal");
+    if (!builtin_module)
+	    Py_FatalError("mashal module lost");
+    Py_INCREF(builtin_module);
+    if (PyModule_AddObject(importlib, "marshal", builtin_module) < 0)
+	    Py_FatalError("could not add marshal to _importlib");
+
+    builtin_module = PyDict_GetItemString(modules, PyOS_MODNAME);
+    if (!builtin_module)
+	    Py_FatalError("_os module lost");
+    Py_INCREF(builtin_module);
+    if (PyModule_AddObject(importlib, "_os", builtin_module) < 0)
+	    Py_FatalError("could not add _os to _importlib");
+
+
+    Py_DECREF(importlib);
 }
 
 
@@ -1653,7 +1697,6 @@
 #endif /* HAVE_STAT */
 
 
-static int init_builtin(char *); /* Forward */
 
 /* Load an external module using the default search path and return
    its module object WITH INCREMENTED REFERENCE COUNT */


More information about the Python-3000-checkins mailing list