@@ -65,7 +65,7 @@ static _PyInitError init_sys_streams(PyInterpreterState *interp);
6565static _PyInitError initsigs (void );
6666static void call_py_exitfuncs (PyInterpreterState * );
6767static void wait_for_thread_shutdown (void );
68- static void call_ll_exitfuncs (void );
68+ static void call_ll_exitfuncs (_PyRuntimeState * runtime );
6969
7070int _Py_UnhandledKeyboardInterrupt = 0 ;
7171_PyRuntimeState _PyRuntime = _PyRuntimeState_INIT ;
@@ -1131,23 +1131,23 @@ flush_std_files(void)
11311131int
11321132Py_FinalizeEx (void )
11331133{
1134- PyInterpreterState * interp ;
1135- PyThreadState * tstate ;
11361134 int status = 0 ;
11371135
1138- if (!_PyRuntime .initialized )
1136+ _PyRuntimeState * runtime = & _PyRuntime ;
1137+ if (!runtime -> initialized ) {
11391138 return status ;
1139+ }
11401140
11411141 // Wrap up existing "threading"-module-created, non-daemon threads.
11421142 wait_for_thread_shutdown ();
11431143
1144- /* Get current thread state and interpreter pointer */
1145- tstate = _PyThreadState_GET ();
1146- interp = tstate -> interp ;
1147-
11481144 // Make any remaining pending calls.
11491145 _Py_FinishPendingCalls ();
11501146
1147+ /* Get current thread state and interpreter pointer */
1148+ PyThreadState * tstate = _PyThreadState_GET ();
1149+ PyInterpreterState * interp = tstate -> interp ;
1150+
11511151 /* The interpreter is still entirely intact at this point, and the
11521152 * exit funcs may be relying on that. In particular, if some thread
11531153 * or exit func is still waiting to do an import, the import machinery
@@ -1174,9 +1174,9 @@ Py_FinalizeEx(void)
11741174
11751175 /* Remaining threads (e.g. daemon threads) will automatically exit
11761176 after taking the GIL (in PyEval_RestoreThread()). */
1177- _PyRuntime . finalizing = tstate ;
1178- _PyRuntime . initialized = 0 ;
1179- _PyRuntime . core_initialized = 0 ;
1177+ runtime -> finalizing = tstate ;
1178+ runtime -> initialized = 0 ;
1179+ runtime -> core_initialized = 0 ;
11801180
11811181 /* Flush sys.stdout and sys.stderr */
11821182 if (flush_std_files () < 0 ) {
@@ -1294,7 +1294,7 @@ Py_FinalizeEx(void)
12941294 PyFloat_Fini ();
12951295 PyDict_Fini ();
12961296 PySlice_Fini ();
1297- _PyGC_Fini ();
1297+ _PyGC_Fini (runtime );
12981298 _Py_HashRandomization_Fini ();
12991299 _PyArg_Fini ();
13001300 PyAsyncGen_Fini ();
@@ -1314,7 +1314,7 @@ Py_FinalizeEx(void)
13141314 PyGrammar_RemoveAccelerators (& _PyParser_Grammar );
13151315
13161316 /* Cleanup auto-thread-state */
1317- _PyGILState_Fini ();
1317+ _PyGILState_Fini (runtime );
13181318
13191319 /* Delete current thread. After this, many C API calls become crashy. */
13201320 PyThreadState_Swap (NULL );
@@ -1336,7 +1336,7 @@ Py_FinalizeEx(void)
13361336 }
13371337#endif
13381338
1339- call_ll_exitfuncs ();
1339+ call_ll_exitfuncs (runtime );
13401340
13411341 _PyRuntime_Finalize ();
13421342 return status ;
@@ -2223,10 +2223,11 @@ int Py_AtExit(void (*func)(void))
22232223}
22242224
22252225static void
2226- call_ll_exitfuncs (void )
2226+ call_ll_exitfuncs (_PyRuntimeState * runtime )
22272227{
2228- while (_PyRuntime .nexitfuncs > 0 )
2229- (* _PyRuntime .exitfuncs [-- _PyRuntime .nexitfuncs ])();
2228+ while (runtime -> nexitfuncs > 0 ) {
2229+ (* runtime -> exitfuncs [-- runtime -> nexitfuncs ])();
2230+ }
22302231
22312232 fflush (stdout );
22322233 fflush (stderr );
0 commit comments