Skip to content

Commit 4f04511

Browse files
author
christian.heimes
committed
Issue 3723: Fixed initialization of subinterpreters
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. git-svn-id: http://svn.python.org/projects/python/branches/py3k@67057 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 2b32aa7 commit 4f04511

9 files changed

Lines changed: 43 additions & 3 deletions

File tree

Demo/embed/importexc.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#include <Python.h>
22

3-
char* cmd = "import exceptions";
3+
#if 0
4+
char* cmd = "import codecs, encodings.utf_8, types; print(types)";
5+
#else
6+
char* cmd = "import types; print(types)";
7+
#endif
48

59
int main()
610
{
11+
printf("Initialize interpreter\n");
712
Py_Initialize();
813
PyEval_InitThreads();
914
PyRun_SimpleString(cmd);
1015
Py_EndInterpreter(PyThreadState_Get());
1116

17+
printf("\nInitialize subinterpreter\n");
1218
Py_NewInterpreter();
1319
PyRun_SimpleString(cmd);
1420
Py_Finalize();

Include/pystate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ typedef struct _is {
2727
PyObject *codec_search_path;
2828
PyObject *codec_search_cache;
2929
PyObject *codec_error_registry;
30+
int codecs_initialized;
3031

3132
#ifdef HAVE_DLOPEN
3233
int dlopenflags;

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ What's New in Python 3.0 beta 5
1515
Core and Builtins
1616
-----------------
1717

18+
- Issue 3723: Fixed initialization of subinterpreters.
19+
1820
- Issue #4213: The file system encoding is now normalized by the
1921
codec subsystem, for example UTF-8 is turned into utf-8.
2022

Objects/unicodeobject.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,19 @@ PyObject *PyUnicode_AsEncodedString(PyObject *unicode,
13461346
#endif
13471347
else if (strcmp(encoding, "ascii") == 0)
13481348
return PyUnicode_AsASCIIString(unicode);
1349+
/* During bootstrap, we may need to find the encodings
1350+
package, to load the file system encoding, and require the
1351+
file system encoding in order to load the encodings
1352+
package.
1353+
1354+
Break out of this dependency by assuming that the path to
1355+
the encodings module is ASCII-only. XXX could try wcstombs
1356+
instead, if the file system encoding is the locale's
1357+
encoding. */
1358+
else if (Py_FileSystemDefaultEncoding &&
1359+
strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 &&
1360+
!PyThreadState_GET()->interp->codecs_initialized)
1361+
return PyUnicode_AsASCIIString(unicode);
13491362
}
13501363

13511364
/* Encode via the codec registry */

Python/bltinmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2282,7 +2282,7 @@ static struct PyModuleDef builtinsmodule = {
22822282
PyModuleDef_HEAD_INIT,
22832283
"builtins",
22842284
builtin_doc,
2285-
0,
2285+
-1, /* multiple "initialization" just copies the module dict. */
22862286
builtin_methods,
22872287
NULL,
22882288
NULL,

Python/codecs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,5 +869,6 @@ static int _PyCodecRegistry_Init(void)
869869
return -1;
870870
}
871871
Py_DECREF(mod);
872+
interp->codecs_initialized = 1;
872873
return 0;
873874
}

Python/pystate.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ PyInterpreterState_New(void)
7676
interp->codec_search_path = NULL;
7777
interp->codec_search_cache = NULL;
7878
interp->codec_error_registry = NULL;
79+
interp->codecs_initialized = 0;
7980
#ifdef HAVE_DLOPEN
8081
#ifdef RTLD_NOW
8182
interp->dlopenflags = RTLD_NOW;

Python/pythonrun.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,16 +562,32 @@ Py_NewInterpreter(void)
562562
goto handle_error;
563563
Py_INCREF(interp->builtins);
564564
}
565+
566+
/* initialize builtin exceptions */
567+
_PyExc_Init();
568+
565569
sysmod = _PyImport_FindExtension("sys", "sys");
566570
if (bimod != NULL && sysmod != NULL) {
571+
PyObject *pstderr;
567572
interp->sysdict = PyModule_GetDict(sysmod);
568573
if (interp->sysdict == NULL)
569574
goto handle_error;
570575
Py_INCREF(interp->sysdict);
571576
PySys_SetPath(Py_GetPath());
572577
PyDict_SetItemString(interp->sysdict, "modules",
573578
interp->modules);
579+
/* Set up a preliminary stderr printer until we have enough
580+
infrastructure for the io module in place. */
581+
pstderr = PyFile_NewStdPrinter(fileno(stderr));
582+
if (pstderr == NULL)
583+
Py_FatalError("Py_Initialize: can't set preliminary stderr");
584+
PySys_SetObject("stderr", pstderr);
585+
PySys_SetObject("__stderr__", pstderr);
586+
574587
_PyImportHooks_Init();
588+
if (initstdio() < 0)
589+
Py_FatalError(
590+
"Py_Initialize: can't initialize sys standard streams");
575591
initmain();
576592
if (!Py_NoSiteFlag)
577593
initsite();

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ static struct PyModuleDef sysmodule = {
12311231
PyModuleDef_HEAD_INIT,
12321232
"sys",
12331233
sys_doc,
1234-
0,
1234+
-1, /* multiple "initialization" just copies the module dict. */
12351235
sys_methods,
12361236
NULL,
12371237
NULL,

0 commit comments

Comments
 (0)