File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -113,6 +113,8 @@ PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
113113PyAPI_FUNC (PyObject * ) PyState_FindModule (struct PyModuleDef * );
114114
115115PyAPI_FUNC (PyThreadState * ) PyThreadState_New (PyInterpreterState * );
116+ PyAPI_FUNC (PyThreadState * ) _PyThreadState_Prealloc (PyInterpreterState * );
117+ PyAPI_FUNC (void ) _PyThreadState_Init (PyThreadState * );
116118PyAPI_FUNC (void ) PyThreadState_Clear (PyThreadState * );
117119PyAPI_FUNC (void ) PyThreadState_Delete (PyThreadState * );
118120#ifdef WITH_THREAD
Original file line number Diff line number Diff line change @@ -12,6 +12,9 @@ What's New in Python 3.1.2?
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 @@ -423,6 +423,7 @@ struct bootstate {
423423 PyObject * func ;
424424 PyObject * args ;
425425 PyObject * keyw ;
426+ PyThreadState * tstate ;
426427};
427428
428429static void
@@ -432,8 +433,9 @@ t_bootstrap(void *boot_raw)
432433 PyThreadState * tstate ;
433434 PyObject * res ;
434435
435- tstate = PyThreadState_New (boot -> interp );
436-
436+ tstate = boot -> tstate ;
437+ tstate -> thread_id = PyThread_get_thread_ident ();
438+ _PyThreadState_Init (tstate );
437439 PyEval_AcquireThread (tstate );
438440 res = PyEval_CallObjectWithKeywords (
439441 boot -> func , boot -> args , boot -> keyw );
@@ -496,6 +498,11 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
496498 boot -> func = func ;
497499 boot -> args = args ;
498500 boot -> keyw = keyw ;
501+ boot -> tstate = _PyThreadState_Prealloc (boot -> interp );
502+ if (boot -> tstate == NULL ) {
503+ PyMem_DEL (boot );
504+ return PyErr_NoMemory ();
505+ }
499506 Py_INCREF (func );
500507 Py_INCREF (args );
501508 Py_XINCREF (keyw );
@@ -506,6 +513,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
506513 Py_DECREF (func );
507514 Py_DECREF (args );
508515 Py_XDECREF (keyw );
516+ PyThreadState_Clear (boot -> tstate );
509517 PyMem_DEL (boot );
510518 return NULL ;
511519 }
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