Skip to content

Commit 9b577cd

Browse files
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
1 parent 313f92a commit 9b577cd

File tree

1 file changed

+2
-38
lines changed

1 file changed

+2
-38
lines changed

Python/pystate.c

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ static PyThreadState *
634634
new_threadstate(PyInterpreterState *interp, int init)
635635
{
636636
_PyRuntimeState *runtime = interp->runtime;
637-
PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
637+
PyThreadState *tstate = (PyThreadState *)PyMem_RawCalloc(1, sizeof(PyThreadState));
638638
if (tstate == NULL) {
639639
return NULL;
640640
}
@@ -643,49 +643,16 @@ new_threadstate(PyInterpreterState *interp, int init)
643643

644644
tstate->recursion_limit = interp->ceval.recursion_limit;
645645
tstate->recursion_remaining = interp->ceval.recursion_limit;
646-
tstate->recursion_headroom = 0;
647-
tstate->tracing = 0;
648-
tstate->root_cframe.use_tracing = 0;
649-
tstate->root_cframe.current_frame = NULL;
650646
tstate->cframe = &tstate->root_cframe;
651-
tstate->gilstate_counter = 0;
652-
tstate->async_exc = NULL;
653647
tstate->thread_id = PyThread_get_thread_ident();
654648
#ifdef PY_HAVE_THREAD_NATIVE_ID
655649
tstate->native_thread_id = PyThread_get_thread_native_id();
656-
#else
657-
tstate->native_thread_id = 0;
658650
#endif
659651

660-
tstate->dict = NULL;
661-
662-
tstate->curexc_type = NULL;
663-
tstate->curexc_value = NULL;
664-
tstate->curexc_traceback = NULL;
665-
666-
tstate->exc_state.exc_type = NULL;
667-
tstate->exc_state.exc_value = NULL;
668-
tstate->exc_state.exc_traceback = NULL;
669-
tstate->exc_state.previous_item = NULL;
670652
tstate->exc_info = &tstate->exc_state;
671653

672-
tstate->c_profilefunc = NULL;
673-
tstate->c_tracefunc = NULL;
674-
tstate->c_profileobj = NULL;
675-
tstate->c_traceobj = NULL;
676-
677-
tstate->trash_delete_nesting = 0;
678-
tstate->trash_delete_later = NULL;
679-
tstate->on_delete = NULL;
680-
tstate->on_delete_data = NULL;
681-
682-
tstate->coroutine_origin_tracking_depth = 0;
683-
684-
tstate->async_gen_firstiter = NULL;
685-
tstate->async_gen_finalizer = NULL;
686-
687-
tstate->context = NULL;
688654
tstate->context_ver = 1;
655+
689656
tstate->datastack_chunk = allocate_chunk(DATA_STACK_CHUNK_SIZE, NULL);
690657
if (tstate->datastack_chunk == NULL) {
691658
PyMem_RawFree(tstate);
@@ -694,16 +661,13 @@ new_threadstate(PyInterpreterState *interp, int init)
694661
/* If top points to entry 0, then _PyThreadState_PopFrame will try to pop this chunk */
695662
tstate->datastack_top = &tstate->datastack_chunk->data[1];
696663
tstate->datastack_limit = (PyObject **)(((char *)tstate->datastack_chunk) + DATA_STACK_CHUNK_SIZE);
697-
/* Mark trace_info as uninitialized */
698-
tstate->trace_info.code = NULL;
699664

700665
if (init) {
701666
_PyThreadState_Init(tstate);
702667
}
703668

704669
HEAD_LOCK(runtime);
705670
tstate->id = ++interp->threads.next_unique_id;
706-
tstate->prev = NULL;
707671
tstate->next = interp->threads.head;
708672
if (tstate->next)
709673
tstate->next->prev = tstate;

0 commit comments

Comments
 (0)