bpo-43268: Pass interp rather than tstate to internal functions (GH-24580)

Pass the current interpreter (interp) rather than the current Python
thread state (tstate) to internal functions which only use the
interpreter.

Modified functions:

* _PyXXX_Fini() and _PyXXX_ClearFreeList() functions
* _PyEval_SignalAsyncExc(), make_pending_calls()
* _PySys_GetObject(), sys_set_object(), sys_set_object_id(), sys_set_object_str()
* should_audit(), set_flags_from_config(), make_flags()
* _PyAtExit_Call()
* init_stdio_encoding()
* etc.
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index f990fa6..ec77084 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -574,7 +574,7 @@ init_interp_create_gil(PyThreadState *tstate)
 
     /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is
        only called here. */
-    _PyEval_FiniGIL(tstate);
+    _PyEval_FiniGIL(tstate->interp);
 
     /* Auto-thread-state API */
     status = _PyGILState_Init(tstate);
@@ -624,12 +624,12 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
 
 
 static PyStatus
-pycore_init_types(PyThreadState *tstate)
+pycore_init_types(PyInterpreterState *interp)
 {
     PyStatus status;
-    int is_main_interp = _Py_IsMainInterpreter(tstate->interp);
+    int is_main_interp = _Py_IsMainInterpreter(interp);
 
-    status = _PyGC_Init(tstate);
+    status = _PyGC_Init(interp);
     if (_PyStatus_EXCEPTION(status)) {
         return status;
     }
@@ -637,7 +637,7 @@ pycore_init_types(PyThreadState *tstate)
     // Create the empty tuple singleton. It must be created before the first
     // PyType_Ready() call since PyType_Ready() creates tuples, for tp_bases
     // for example.
-    status = _PyTuple_Init(tstate);
+    status = _PyTuple_Init(interp);
     if (_PyStatus_EXCEPTION(status)) {
         return status;
     }
@@ -649,21 +649,21 @@ pycore_init_types(PyThreadState *tstate)
         }
     }
 
-    if (!_PyLong_Init(tstate)) {
+    if (!_PyLong_Init(interp)) {
         return _PyStatus_ERR("can't init longs");
     }
 
-    status = _PyUnicode_Init(tstate);
+    status = _PyUnicode_Init(interp);
     if (_PyStatus_EXCEPTION(status)) {
         return status;
     }
 
-    status = _PyBytes_Init(tstate);
+    status = _PyBytes_Init(interp);
     if (_PyStatus_EXCEPTION(status)) {
         return status;
     }
 
-    status = _PyExc_Init(tstate);
+    status = _PyExc_Init(interp);
     if (_PyStatus_EXCEPTION(status)) {
         return status;
     }
@@ -689,11 +689,11 @@ pycore_init_types(PyThreadState *tstate)
         }
     }
 
-    if (_PyWarnings_InitState(tstate) < 0) {
+    if (_PyWarnings_InitState(interp) < 0) {
         return _PyStatus_ERR("can't initialize warnings");
     }
 
-    status = _PyAtExit_Init(tstate);
+    status = _PyAtExit_Init(interp);
     if (_PyStatus_EXCEPTION(status)) {
         return status;
     }
@@ -703,16 +703,13 @@ pycore_init_types(PyThreadState *tstate)
 
 
 static PyStatus
-pycore_init_builtins(PyThreadState *tstate)
+pycore_init_builtins(PyInterpreterState *interp)
 {
-    assert(!_PyErr_Occurred(tstate));
-
-    PyObject *bimod = _PyBuiltin_Init(tstate);
+    PyObject *bimod = _PyBuiltin_Init(interp);
     if (bimod == NULL) {
         goto error;
     }
 
-    PyInterpreterState *interp = tstate->interp;
     if (_PyImport_FixupBuiltin(bimod, "builtins", interp->modules) < 0) {
         goto error;
     }
@@ -743,8 +740,6 @@ pycore_init_builtins(PyThreadState *tstate)
     }
     interp->import_func = Py_NewRef(import_func);
 
-    assert(!_PyErr_Occurred(tstate));
-
     return _PyStatus_OK();
 
 error:
@@ -759,7 +754,7 @@ pycore_interp_init(PyThreadState *tstate)
     PyStatus status;
     PyObject *sysmod = NULL;
 
-    status = pycore_init_types(tstate);
+    status = pycore_init_types(tstate->interp);
     if (_PyStatus_EXCEPTION(status)) {
         goto done;
     }
@@ -769,11 +764,15 @@ pycore_interp_init(PyThreadState *tstate)
         goto done;
     }
 
-    status = pycore_init_builtins(tstate);
+    assert(!_PyErr_Occurred(tstate));
+
+    status = pycore_init_builtins(tstate->interp);
     if (_PyStatus_EXCEPTION(status)) {
         goto done;
     }
 
+    assert(!_PyErr_Occurred(tstate));
+
     const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
     if (config->_install_importlib) {
         /* This call sets up builtin and frozen import support */
@@ -1464,7 +1463,7 @@ finalize_modules(PyThreadState *tstate)
 
     // Dump GC stats before it's too late, since it uses the warnings
     // machinery.
-    _PyGC_DumpShutdownStats(tstate);
+    _PyGC_DumpShutdownStats(interp);
 
     if (weaklist != NULL) {
         // Now, if there are any modules left alive, clear their globals to
@@ -1570,27 +1569,27 @@ flush_std_files(void)
 
 
 static void
-finalize_interp_types(PyThreadState *tstate)
+finalize_interp_types(PyInterpreterState *interp)
 {
-    _PyExc_Fini(tstate);
-    _PyFrame_Fini(tstate);
-    _PyAsyncGen_Fini(tstate);
-    _PyContext_Fini(tstate);
-    _PyType_Fini(tstate);
+    _PyExc_Fini(interp);
+    _PyFrame_Fini(interp);
+    _PyAsyncGen_Fini(interp);
+    _PyContext_Fini(interp);
+    _PyType_Fini(interp);
     // Call _PyUnicode_ClearInterned() before _PyDict_Fini() since it uses
     // a dict internally.
-    _PyUnicode_ClearInterned(tstate);
+    _PyUnicode_ClearInterned(interp);
 
-    _PyDict_Fini(tstate);
-    _PyList_Fini(tstate);
-    _PyTuple_Fini(tstate);
+    _PyDict_Fini(interp);
+    _PyList_Fini(interp);
+    _PyTuple_Fini(interp);
 
-    _PySlice_Fini(tstate);
+    _PySlice_Fini(interp);
 
-    _PyBytes_Fini(tstate);
-    _PyUnicode_Fini(tstate);
-    _PyFloat_Fini(tstate);
-    _PyLong_Fini(tstate);
+    _PyBytes_Fini(interp);
+    _PyUnicode_Fini(interp);
+    _PyFloat_Fini(interp);
+    _PyLong_Fini(interp);
 }
 
 
@@ -1615,16 +1614,16 @@ finalize_interp_clear(PyThreadState *tstate)
         _Py_ClearFileSystemEncoding();
     }
 
-    finalize_interp_types(tstate);
+    finalize_interp_types(tstate->interp);
 }
 
 
 static void
-finalize_interp_delete(PyThreadState *tstate)
+finalize_interp_delete(PyInterpreterState *interp)
 {
-    if (_Py_IsMainInterpreter(tstate->interp)) {
+    if (_Py_IsMainInterpreter(interp)) {
         /* Cleanup auto-thread-state */
-        _PyGILState_Fini(tstate);
+        _PyGILState_Fini(interp);
     }
 
     /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can
@@ -1633,7 +1632,7 @@ finalize_interp_delete(PyThreadState *tstate)
        created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be
        called multiple times. */
 
-    PyInterpreterState_Delete(tstate->interp);
+    PyInterpreterState_Delete(interp);
 }
 
 
@@ -1666,7 +1665,7 @@ Py_FinalizeEx(void)
      * the threads created via Threading.
      */
 
-    _PyAtExit_Call(tstate);
+    _PyAtExit_Call(tstate->interp);
 
     /* Copy the core config, PyInterpreterState_Delete() free
        the core config memory */
@@ -1779,7 +1778,7 @@ Py_FinalizeEx(void)
 #endif /* Py_TRACE_REFS */
 
     finalize_interp_clear(tstate);
-    finalize_interp_delete(tstate);
+    finalize_interp_delete(tstate->interp);
 
 #ifdef Py_TRACE_REFS
     /* Display addresses (& refcnts) of all objects still alive.
@@ -1954,7 +1953,7 @@ Py_EndInterpreter(PyThreadState *tstate)
     // Wrap up existing "threading"-module-created, non-daemon threads.
     wait_for_thread_shutdown(tstate);
 
-    _PyAtExit_Call(tstate);
+    _PyAtExit_Call(tstate->interp);
 
     if (tstate != interp->tstate_head || tstate->next != NULL) {
         Py_FatalError("not the last thread");
@@ -1963,7 +1962,7 @@ Py_EndInterpreter(PyThreadState *tstate)
     finalize_modules(tstate);
 
     finalize_interp_clear(tstate);
-    finalize_interp_delete(tstate);
+    finalize_interp_delete(tstate->interp);
 }
 
 /* Add the __main__ module */