File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -115,6 +115,8 @@ PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
115115PyAPI_FUNC (PyObject * ) PyState_FindModule (struct PyModuleDef * );
116116
117117PyAPI_FUNC (PyThreadState * ) PyThreadState_New (PyInterpreterState * );
118+ PyAPI_FUNC (PyThreadState * ) _PyThreadState_Prealloc (PyInterpreterState * );
119+ PyAPI_FUNC (void ) _PyThreadState_Init (PyThreadState * );
118120PyAPI_FUNC (void ) PyThreadState_Clear (PyThreadState * );
119121PyAPI_FUNC (void ) PyThreadState_Delete (PyThreadState * );
120122#ifdef WITH_THREAD
Original file line number Diff line number Diff line change @@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1?
1212Core and Builtins
1313-----------------
1414
15+ - Issue #7544: Preallocate thread memory before creating the thread to avoid
16+ a fatal error in low memory condition.
17+
1518- Issue #7820: The parser tokenizer restores all bytes in the right if
1619 the BOM check fails.
1720
Original file line number Diff line number Diff line change @@ -695,6 +695,7 @@ struct bootstate {
695695 PyObject * func ;
696696 PyObject * args ;
697697 PyObject * keyw ;
698+ PyThreadState * tstate ;
698699};
699700
700701static void
@@ -704,8 +705,9 @@ t_bootstrap(void *boot_raw)
704705 PyThreadState * tstate ;
705706 PyObject * res ;
706707
707- tstate = PyThreadState_New (boot -> interp );
708-
708+ tstate = boot -> tstate ;
709+ tstate -> thread_id = PyThread_get_thread_ident ();
710+ _PyThreadState_Init (tstate );
709711 PyEval_AcquireThread (tstate );
710712 nb_threads ++ ;
711713 res = PyEval_CallObjectWithKeywords (
@@ -770,6 +772,11 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
770772 boot -> func = func ;
771773 boot -> args = args ;
772774 boot -> keyw = keyw ;
775+ boot -> tstate = _PyThreadState_Prealloc (boot -> interp );
776+ if (boot -> tstate == NULL ) {
777+ PyMem_DEL (boot );
778+ return PyErr_NoMemory ();
779+ }
773780 Py_INCREF (func );
774781 Py_INCREF (args );
775782 Py_XINCREF (keyw );
@@ -780,6 +787,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
780787 Py_DECREF (func );
781788 Py_DECREF (args );
782789 Py_XDECREF (keyw );
790+ PyThreadState_Clear (boot -> tstate );
783791 PyMem_DEL (boot );
784792 return NULL ;
785793 }
Original file line number Diff line number Diff line change @@ -157,8 +157,8 @@ threadstate_getframe(PyThreadState *self)
157157 return self -> frame ;
158158}
159159
160- PyThreadState *
161- PyThreadState_New (PyInterpreterState * interp )
160+ static PyThreadState *
161+ new_threadstate (PyInterpreterState * interp , int init )
162162{
163163 PyThreadState * tstate = (PyThreadState * )malloc (sizeof (PyThreadState ));
164164
@@ -198,9 +198,8 @@ PyThreadState_New(PyInterpreterState *interp)
198198 tstate -> c_profileobj = NULL ;
199199 tstate -> c_traceobj = NULL ;
200200
201- #ifdef WITH_THREAD
202- _PyGILState_NoteThreadState (tstate );
203- #endif
201+ if (init )
202+ _PyThreadState_Init (tstate );
204203
205204 HEAD_LOCK ();
206205 tstate -> next = interp -> tstate_head ;
@@ -211,6 +210,26 @@ PyThreadState_New(PyInterpreterState *interp)
211210 return tstate ;
212211}
213212
213+ PyThreadState *
214+ PyThreadState_New (PyInterpreterState * interp )
215+ {
216+ return new_threadstate (interp , 1 );
217+ }
218+
219+ PyThreadState *
220+ _PyThreadState_Prealloc (PyInterpreterState * interp )
221+ {
222+ return new_threadstate (interp , 0 );
223+ }
224+
225+ void
226+ _PyThreadState_Init (PyThreadState * tstate )
227+ {
228+ #ifdef WITH_THREAD
229+ _PyGILState_NoteThreadState (tstate );
230+ #endif
231+ }
232+
214233PyObject *
215234PyState_FindModule (struct PyModuleDef * m )
216235{
You can’t perform that action at this time.
0 commit comments