From 707b5d166fd65ace80e6bda3987be694c1dc54e8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 10 Jan 2010 00:50:17 +0100 Subject: [PATCH] thread: prealloc PyThreadState before creating the thread Raise a MemoryError if the allocation fail, instead of raising a *fatal* Python error. Fix issue #7544 --- Modules/threadmodule.c | 11 +++++++++-- 1 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Modules/threadmodule.c b/Modules/threadmodule.c index c682af2..a1faf9b 100644 --- a/Modules/threadmodule.c +++ b/Modules/threadmodule.c @@ -427,6 +427,7 @@ struct bootstate { PyObject *func; PyObject *args; PyObject *keyw; + PyThreadState *tstate; }; static void @@ -436,8 +437,8 @@ t_bootstrap(void *boot_raw) PyThreadState *tstate; PyObject *res; - tstate = PyThreadState_New(boot->interp); - + tstate = boot->tstate; + tstate->thread_id = PyThread_get_thread_ident(); PyEval_AcquireThread(tstate); nb_threads++; res = PyEval_CallObjectWithKeywords( @@ -502,6 +503,11 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) boot->func = func; boot->args = args; boot->keyw = keyw; + boot->tstate = PyThreadState_New(boot->interp); + if (boot->tstate == NULL) { + PyMem_DEL(boot); + return PyErr_NoMemory(); + } Py_INCREF(func); Py_INCREF(args); Py_XINCREF(keyw); @@ -512,6 +518,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) Py_DECREF(func); Py_DECREF(args); Py_XDECREF(keyw); + PyThreadState_Clear(boot->tstate); PyMem_DEL(boot); return NULL; } -- 1.6.5.7