]> granicus.if.org Git - python/commitdiff
Issue 3723: Fixed initialization of subinterpreters
authorChristian Heimes <christian@cheimes.de>
Thu, 30 Oct 2008 21:48:26 +0000 (21:48 +0000)
committerChristian Heimes <christian@cheimes.de>
Thu, 30 Oct 2008 21:48:26 +0000 (21:48 +0000)
The patch fixes several issues with Py_NewInterpreter as well as the demo for multiple subinterpreters.
Most of the patch was written by MvL with help from Benjamin, Amaury and me. Graham Dumpleton has verified that this patch fixes an issue with mod_wsgi.

Demo/embed/importexc.c
Include/pystate.h
Misc/NEWS
Objects/unicodeobject.c
Python/bltinmodule.c
Python/codecs.c
Python/pystate.c
Python/pythonrun.c
Python/sysmodule.c

index 375ce1b686954efb15b2e813385b957b297a2c3d..59b1d01d87be31cbca494624745969bb88c162ee 100644 (file)
@@ -1,14 +1,20 @@
 #include <Python.h>
 
-char* cmd = "import exceptions";
+#if 0
+char* cmd = "import codecs, encodings.utf_8, types; print(types)";
+#else
+char* cmd = "import types; print(types)";
+#endif
 
 int main()
 {
+       printf("Initialize interpreter\n");
        Py_Initialize();
        PyEval_InitThreads();
        PyRun_SimpleString(cmd);
        Py_EndInterpreter(PyThreadState_Get());
 
+       printf("\nInitialize subinterpreter\n");
        Py_NewInterpreter();
        PyRun_SimpleString(cmd);
        Py_Finalize();
index 8508da0554bfaea44372ad301c96184e4439747d..e02df88f8c652490a490eb3a5c869b71e58a889d 100644 (file)
@@ -27,6 +27,7 @@ typedef struct _is {
     PyObject *codec_search_path;
     PyObject *codec_search_cache;
     PyObject *codec_error_registry;
+    int codecs_initialized;
 
 #ifdef HAVE_DLOPEN
     int dlopenflags;
index 72a482eff64f3c899963f7b57de342c60f6c1b25..b8d17c123c97569a6cf5da52023028db0b6984c6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,8 @@ What's New in Python 3.0 beta 5
 Core and Builtins
 -----------------
 
+- Issue 3723: Fixed initialization of subinterpreters.
+
 - Issue #4213: The file system encoding is now normalized by the
   codec subsystem, for example UTF-8 is turned into utf-8.
 
index ecd44cb78831b608f2ff1f0afe0192e992797551..2beb0984b45a64eb5816a8a4cb697c16c6a38cd8 100644 (file)
@@ -1346,6 +1346,19 @@ PyObject *PyUnicode_AsEncodedString(PyObject *unicode,
 #endif
        else if (strcmp(encoding, "ascii") == 0)
            return PyUnicode_AsASCIIString(unicode);
+        /* During bootstrap, we may need to find the encodings
+           package, to load the file system encoding, and require the
+           file system encoding in order to load the encodings
+           package.
+
+           Break out of this dependency by assuming that the path to
+           the encodings module is ASCII-only.  XXX could try wcstombs
+           instead, if the file system encoding is the locale's
+           encoding. */
+        else if (Py_FileSystemDefaultEncoding &&
+                 strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 &&
+                 !PyThreadState_GET()->interp->codecs_initialized)
+           return PyUnicode_AsASCIIString(unicode);            
     }
 
     /* Encode via the codec registry */
index 8159fe84d5851228de7dc2f2e9ebb4bce53154ce..d7f5084a588d53a00d78af3c517f6e25384f550f 100644 (file)
@@ -2282,7 +2282,7 @@ static struct PyModuleDef builtinsmodule = {
        PyModuleDef_HEAD_INIT,
        "builtins",
        builtin_doc,
-       0,
+       -1, /* multiple "initialization" just copies the module dict. */
        builtin_methods,
        NULL,
        NULL,
index 66576c481c9b7737aa9f1a36650454fc80c8515e..ebddc09d7b0336fc2d5e19edd1f084863d6f394e 100644 (file)
@@ -869,5 +869,6 @@ static int _PyCodecRegistry_Init(void)
        return -1;
     }
     Py_DECREF(mod);
+    interp->codecs_initialized = 1;
     return 0;
 }
index 94792567187338ef05d31a5c0d22f08f0d41fbf3..2a9b7443f974304bd014df67d2fca6c409898f2d 100644 (file)
@@ -76,6 +76,7 @@ PyInterpreterState_New(void)
                interp->codec_search_path = NULL;
                interp->codec_search_cache = NULL;
                interp->codec_error_registry = NULL;
+               interp->codecs_initialized = 0;
 #ifdef HAVE_DLOPEN
 #ifdef RTLD_NOW
                 interp->dlopenflags = RTLD_NOW;
index 4fb2b3e40d29067fa753f9ae8d48b8d54bf41ab8..61cdf6d7459f00e66b69a5d8a1262b9762f85f17 100644 (file)
@@ -562,8 +562,13 @@ Py_NewInterpreter(void)
                        goto handle_error;
                Py_INCREF(interp->builtins);
        }
+
+       /* initialize builtin exceptions */
+       _PyExc_Init();
+
        sysmod = _PyImport_FindExtension("sys", "sys");
        if (bimod != NULL && sysmod != NULL) {
+               PyObject *pstderr;
                interp->sysdict = PyModule_GetDict(sysmod);
                if (interp->sysdict == NULL)
                        goto handle_error;
@@ -571,7 +576,18 @@ Py_NewInterpreter(void)
                PySys_SetPath(Py_GetPath());
                PyDict_SetItemString(interp->sysdict, "modules",
                                     interp->modules);
+               /* Set up a preliminary stderr printer until we have enough
+                  infrastructure for the io module in place. */
+               pstderr = PyFile_NewStdPrinter(fileno(stderr));
+               if (pstderr == NULL)
+                       Py_FatalError("Py_Initialize: can't set preliminary stderr");
+               PySys_SetObject("stderr", pstderr);
+               PySys_SetObject("__stderr__", pstderr);
+
                _PyImportHooks_Init();
+                if (initstdio() < 0)
+                    Py_FatalError(
+                        "Py_Initialize: can't initialize sys standard streams");
                initmain();
                if (!Py_NoSiteFlag)
                        initsite();
index 952f7e59045b6e4df81b232aca507a05c1c9992e..af960bc07001e7d28e184576a5a6613265d58a08 100644 (file)
@@ -1231,7 +1231,7 @@ static struct PyModuleDef sysmodule = {
        PyModuleDef_HEAD_INIT,
        "sys",
        sys_doc,
-       0,
+       -1, /* multiple "initialization" just copies the module dict. */
        sys_methods,
        NULL,
        NULL,