@@ -873,14 +873,29 @@ PyThreadState *
873873PyThreadState_New (PyInterpreterState * interp )
874874{
875875 PyThreadState * tstate = new_threadstate (interp );
876- _PyThreadState_SetCurrent (tstate );
876+ if (tstate ) {
877+ _PyThreadState_SetCurrent (tstate );
878+ if (PySys_Audit ("cpython.PyThreadState_New" , "K" , tstate -> id ) < 0 ) {
879+ PyThreadState_Clear (tstate );
880+ _PyThreadState_DeleteCurrent (tstate );
881+ return NULL ;
882+ }
883+ }
877884 return tstate ;
878885}
879886
880887PyThreadState *
881888_PyThreadState_Prealloc (PyInterpreterState * interp )
882889{
883- return new_threadstate (interp );
890+ PyThreadState * tstate = new_threadstate (interp );
891+ if (tstate ) {
892+ if (PySys_Audit ("cpython.PyThreadState_New" , "K" , tstate -> id ) < 0 ) {
893+ PyThreadState_Clear (tstate );
894+ _PyThreadState_Delete (tstate , 0 );
895+ return NULL ;
896+ }
897+ }
898+ return tstate ;
884899}
885900
886901// We keep this around for (accidental) stable ABI compatibility.
@@ -1028,6 +1043,10 @@ _PyInterpreterState_ClearModules(PyInterpreterState *interp)
10281043void
10291044PyThreadState_Clear (PyThreadState * tstate )
10301045{
1046+ if (PySys_Audit ("cpython.PyThreadState_Clear" , "K" , tstate -> id ) < 0 ) {
1047+ PyErr_WriteUnraisable (NULL );
1048+ }
1049+
10311050 int verbose = _PyInterpreterState_GetConfig (tstate -> interp )-> verbose ;
10321051
10331052 if (verbose && tstate -> cframe -> current_frame != NULL ) {
@@ -1545,16 +1564,16 @@ _PyGILState_Init(_PyRuntimeState *runtime)
15451564PyStatus
15461565_PyGILState_SetTstate (PyThreadState * tstate )
15471566{
1567+ /* must init with valid states */
1568+ assert (tstate != NULL );
1569+ assert (tstate -> interp != NULL );
1570+
15481571 if (!_Py_IsMainInterpreter (tstate -> interp )) {
15491572 /* Currently, PyGILState is shared by all interpreters. The main
15501573 * interpreter is responsible to initialize it. */
15511574 return _PyStatus_OK ();
15521575 }
15531576
1554- /* must init with valid states */
1555- assert (tstate != NULL );
1556- assert (tstate -> interp != NULL );
1557-
15581577 struct _gilstate_runtime_state * gilstate = & tstate -> interp -> runtime -> gilstate ;
15591578
15601579 gilstate -> autoInterpreterState = tstate -> interp ;
0 commit comments