[Python-checkins] bpo-46008: Use PyMem_RawCalloc() to allocate PyThreadState. (GH-29972)

miss-islington webhook-mailer at python.org
Tue Dec 7 16:38:02 EST 2021


https://github.com/python/cpython/commit/9b577cd01f66512b503115c0fdbf0734edfd5f8a
commit: 9b577cd01f66512b503115c0fdbf0734edfd5f8a
branch: main
author: Eric Snow <ericsnowcurrently at gmail.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-12-07T13:37:51-08:00
summary:

bpo-46008: Use PyMem_RawCalloc() to allocate PyThreadState. (GH-29972)



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

files:
M Python/pystate.c

diff --git a/Python/pystate.c b/Python/pystate.c
index 4dd4cab249725..49acd560b386e 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -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;



More information about the Python-checkins mailing list