]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46008: Use PyMem_RawCalloc() to allocate PyThreadState. (GH-29972)
authorEric Snow <ericsnowcurrently@gmail.com>
Tue, 7 Dec 2021 21:37:51 +0000 (14:37 -0700)
committerGitHub <noreply@github.com>
Tue, 7 Dec 2021 21:37:51 +0000 (13:37 -0800)
Doing so allows us to stop assigning various fields to `NULL` and 0.  It also more closely matches the behavior of a static initializer.

Automerge-Triggered-By: GH:ericsnowcurrently
Python/pystate.c

index 4dd4cab24972515aa01a10b00d4c0e96987d062f..49acd560b386ed459229c2811cbc2a4ae2f1bf0d 100644 (file)
@@ -634,7 +634,7 @@ static PyThreadState *
 new_threadstate(PyInterpreterState *interp, int init)
 {
     _PyRuntimeState *runtime = interp->runtime;
-    PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
+    PyThreadState *tstate = (PyThreadState *)PyMem_RawCalloc(1, sizeof(PyThreadState));
     if (tstate == NULL) {
         return NULL;
     }
@@ -643,49 +643,16 @@ new_threadstate(PyInterpreterState *interp, int init)
 
     tstate->recursion_limit = interp->ceval.recursion_limit;
     tstate->recursion_remaining = interp->ceval.recursion_limit;
-    tstate->recursion_headroom = 0;
-    tstate->tracing = 0;
-    tstate->root_cframe.use_tracing = 0;
-    tstate->root_cframe.current_frame = NULL;
     tstate->cframe = &tstate->root_cframe;
-    tstate->gilstate_counter = 0;
-    tstate->async_exc = NULL;
     tstate->thread_id = PyThread_get_thread_ident();
 #ifdef PY_HAVE_THREAD_NATIVE_ID
     tstate->native_thread_id = PyThread_get_thread_native_id();
-#else
-    tstate->native_thread_id = 0;
 #endif
 
-    tstate->dict = NULL;
-
-    tstate->curexc_type = NULL;
-    tstate->curexc_value = NULL;
-    tstate->curexc_traceback = NULL;
-
-    tstate->exc_state.exc_type = NULL;
-    tstate->exc_state.exc_value = NULL;
-    tstate->exc_state.exc_traceback = NULL;
-    tstate->exc_state.previous_item = NULL;
     tstate->exc_info = &tstate->exc_state;
 
-    tstate->c_profilefunc = NULL;
-    tstate->c_tracefunc = NULL;
-    tstate->c_profileobj = NULL;
-    tstate->c_traceobj = NULL;
-
-    tstate->trash_delete_nesting = 0;
-    tstate->trash_delete_later = NULL;
-    tstate->on_delete = NULL;
-    tstate->on_delete_data = NULL;
-
-    tstate->coroutine_origin_tracking_depth = 0;
-
-    tstate->async_gen_firstiter = NULL;
-    tstate->async_gen_finalizer = NULL;
-
-    tstate->context = NULL;
     tstate->context_ver = 1;
+
     tstate->datastack_chunk = allocate_chunk(DATA_STACK_CHUNK_SIZE, NULL);
     if (tstate->datastack_chunk == NULL) {
         PyMem_RawFree(tstate);
@@ -694,8 +661,6 @@ new_threadstate(PyInterpreterState *interp, int init)
     /* If top points to entry 0, then _PyThreadState_PopFrame will try to pop this chunk */
     tstate->datastack_top = &tstate->datastack_chunk->data[1];
     tstate->datastack_limit = (PyObject **)(((char *)tstate->datastack_chunk) + DATA_STACK_CHUNK_SIZE);
-    /* Mark trace_info as uninitialized */
-    tstate->trace_info.code = NULL;
 
     if (init) {
         _PyThreadState_Init(tstate);
@@ -703,7 +668,6 @@ new_threadstate(PyInterpreterState *interp, int init)
 
     HEAD_LOCK(runtime);
     tstate->id = ++interp->threads.next_unique_id;
-    tstate->prev = NULL;
     tstate->next = interp->threads.head;
     if (tstate->next)
         tstate->next->prev = tstate;