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/_warnings.c b/Python/_warnings.c
index 021400f..2c9a2a7 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -114,9 +114,9 @@ init_filters(void)
 
 /* Initialize the given warnings module state. */
 int
-_PyWarnings_InitState(PyThreadState *tstate)
+_PyWarnings_InitState(PyInterpreterState *interp)
 {
-    WarningsState *st = &tstate->interp->warnings;
+    WarningsState *st = &interp->warnings;
 
     if (st->filters == NULL) {
         st->filters = init_filters();
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 8c4e6e5..dec2984 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -2853,11 +2853,11 @@ static struct PyModuleDef builtinsmodule = {
 
 
 PyObject *
-_PyBuiltin_Init(PyThreadState *tstate)
+_PyBuiltin_Init(PyInterpreterState *interp)
 {
     PyObject *mod, *dict, *debug;
 
-    const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
+    const PyConfig *config = _PyInterpreterState_GetConfig(interp);
 
     if (PyType_Ready(&PyFilter_Type) < 0 ||
         PyType_Ready(&PyMap_Type) < 0 ||
diff --git a/Python/ceval.c b/Python/ceval.c
index 4771a51..7ccb8fc 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -323,10 +323,10 @@ _PyEval_InitGIL(PyThreadState *tstate)
 }
 
 void
-_PyEval_FiniGIL(PyThreadState *tstate)
+_PyEval_FiniGIL(PyInterpreterState *interp)
 {
 #ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
-    if (!_Py_IsMainInterpreter(tstate->interp)) {
+    if (!_Py_IsMainInterpreter(interp)) {
         /* Currently, the GIL is shared by all interpreters,
            and only the main interpreter is responsible to create
            and destroy it. */
@@ -335,9 +335,9 @@ _PyEval_FiniGIL(PyThreadState *tstate)
 #endif
 
 #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
-    struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
+    struct _gil_runtime_state *gil = &interp->ceval.gil;
 #else
-    struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
+    struct _gil_runtime_state *gil = &interp->runtime->ceval.gil;
 #endif
     if (!gil_created(gil)) {
         /* First Py_InitializeFromConfig() call: the GIL doesn't exist
@@ -502,10 +502,9 @@ _PyEval_ReInitThreads(PyThreadState *tstate)
    raised. */
 
 void
-_PyEval_SignalAsyncExc(PyThreadState *tstate)
+_PyEval_SignalAsyncExc(PyInterpreterState *interp)
 {
-    assert(is_tstate_valid(tstate));
-    SIGNAL_ASYNC_EXC(tstate->interp);
+    SIGNAL_ASYNC_EXC(interp);
 }
 
 PyThreadState *
@@ -690,10 +689,8 @@ handle_signals(PyThreadState *tstate)
 }
 
 static int
-make_pending_calls(PyThreadState *tstate)
+make_pending_calls(PyInterpreterState *interp)
 {
-    assert(is_tstate_valid(tstate));
-
     /* only execute pending calls on main thread */
     if (!_Py_ThreadCanHandlePendingCalls()) {
         return 0;
@@ -708,11 +705,11 @@ make_pending_calls(PyThreadState *tstate)
 
     /* unsignal before starting to call callbacks, so that any callback
        added in-between re-signals */
-    UNSIGNAL_PENDING_CALLS(tstate->interp);
+    UNSIGNAL_PENDING_CALLS(interp);
     int res = 0;
 
     /* perform a bounded number of calls, in case of recursion */
-    struct _pending_calls *pending = &tstate->interp->ceval.pending;
+    struct _pending_calls *pending = &interp->ceval.pending;
     for (int i=0; i<NPENDINGCALLS; i++) {
         int (*func)(void *) = NULL;
         void *arg = NULL;
@@ -737,7 +734,7 @@ make_pending_calls(PyThreadState *tstate)
 
 error:
     busy = 0;
-    SIGNAL_PENDING_CALLS(tstate->interp);
+    SIGNAL_PENDING_CALLS(interp);
     return res;
 }
 
@@ -745,6 +742,7 @@ void
 _Py_FinishPendingCalls(PyThreadState *tstate)
 {
     assert(PyGILState_Check());
+    assert(is_tstate_valid(tstate));
 
     struct _pending_calls *pending = &tstate->interp->ceval.pending;
 
@@ -752,7 +750,7 @@ _Py_FinishPendingCalls(PyThreadState *tstate)
         return;
     }
 
-    if (make_pending_calls(tstate) < 0) {
+    if (make_pending_calls(tstate->interp) < 0) {
         PyObject *exc, *val, *tb;
         _PyErr_Fetch(tstate, &exc, &val, &tb);
         PyErr_BadInternalCall();
@@ -769,6 +767,7 @@ Py_MakePendingCalls(void)
     assert(PyGILState_Check());
 
     PyThreadState *tstate = _PyThreadState_GET();
+    assert(is_tstate_valid(tstate));
 
     /* Python signal handler doesn't really queue a callback: it only signals
        that a signal was received, see _PyEval_SignalReceived(). */
@@ -777,7 +776,7 @@ Py_MakePendingCalls(void)
         return res;
     }
 
-    res = make_pending_calls(tstate);
+    res = make_pending_calls(tstate->interp);
     if (res != 0) {
         return res;
     }
@@ -950,7 +949,7 @@ eval_frame_handle_pending(PyThreadState *tstate)
     /* Pending calls */
     struct _ceval_state *ceval2 = &tstate->interp->ceval;
     if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
-        if (make_pending_calls(tstate) != 0) {
+        if (make_pending_calls(tstate->interp) != 0) {
             return -1;
         }
     }
diff --git a/Python/ceval_gil.h b/Python/ceval_gil.h
index 56944b8..9b8b432 100644
--- a/Python/ceval_gil.h
+++ b/Python/ceval_gil.h
@@ -318,7 +318,7 @@ take_gil(PyThreadState *tstate)
 
     /* Don't access tstate if the thread must exit */
     if (tstate->async_exc != NULL) {
-        _PyEval_SignalAsyncExc(tstate);
+        _PyEval_SignalAsyncExc(tstate->interp);
     }
 
     MUTEX_UNLOCK(gil->mutex);
diff --git a/Python/context.c b/Python/context.c
index 6a45621..bf2ba93 100644
--- a/Python/context.c
+++ b/Python/context.c
@@ -1287,9 +1287,9 @@ get_token_missing(void)
 
 
 void
-_PyContext_ClearFreeList(PyThreadState *tstate)
+_PyContext_ClearFreeList(PyInterpreterState *interp)
 {
-    struct _Py_context_state *state = &tstate->interp->context;
+    struct _Py_context_state *state = &interp->context;
     for (; state->numfree; state->numfree--) {
         PyContext *ctx = state->freelist;
         state->freelist = (PyContext *)ctx->ctx_weakreflist;
@@ -1300,14 +1300,14 @@ _PyContext_ClearFreeList(PyThreadState *tstate)
 
 
 void
-_PyContext_Fini(PyThreadState *tstate)
+_PyContext_Fini(PyInterpreterState *interp)
 {
-    if (_Py_IsMainInterpreter(tstate->interp)) {
+    if (_Py_IsMainInterpreter(interp)) {
         Py_CLEAR(_token_missing);
     }
-    _PyContext_ClearFreeList(tstate);
+    _PyContext_ClearFreeList(interp);
 #ifdef Py_DEBUG
-    struct _Py_context_state *state = &tstate->interp->context;
+    struct _Py_context_state *state = &interp->context;
     state->numfree = -1;
 #endif
     _PyHamt_Fini();
diff --git a/Python/import.c b/Python/import.c
index 6189dcf..538db69 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -301,16 +301,16 @@ _PyImport_GetModuleId(struct _Py_Identifier *nameid)
 int
 _PyImport_SetModule(PyObject *name, PyObject *m)
 {
-    PyThreadState *tstate = _PyThreadState_GET();
-    PyObject *modules = tstate->interp->modules;
+    PyInterpreterState *interp = _PyInterpreterState_GET();
+    PyObject *modules = interp->modules;
     return PyObject_SetItem(modules, name, m);
 }
 
 int
 _PyImport_SetModuleString(const char *name, PyObject *m)
 {
-    PyThreadState *tstate = _PyThreadState_GET();
-    PyObject *modules = tstate->interp->modules;
+    PyInterpreterState *interp = _PyInterpreterState_GET();
+    PyObject *modules = interp->modules;
     return PyMapping_SetItemString(modules, name, m);
 }
 
@@ -342,9 +342,8 @@ import_get_module(PyThreadState *tstate, PyObject *name)
 
 
 static int
-import_ensure_initialized(PyThreadState *tstate, PyObject *mod, PyObject *name)
+import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *name)
 {
-    PyInterpreterState *interp = tstate->interp;
     PyObject *spec;
 
     _Py_IDENTIFIER(_lock_unlock_module);
@@ -1530,7 +1529,7 @@ PyImport_GetModule(PyObject *name)
 
     mod = import_get_module(tstate, name);
     if (mod != NULL && mod != Py_None) {
-        if (import_ensure_initialized(tstate, mod, name) < 0) {
+        if (import_ensure_initialized(tstate->interp, mod, name) < 0) {
             Py_DECREF(mod);
             remove_importlib_frames(tstate);
             return NULL;
@@ -1594,7 +1593,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
     }
 
     if (mod != NULL && mod != Py_None) {
-        if (import_ensure_initialized(tstate, mod, name) < 0) {
+        if (import_ensure_initialized(tstate->interp, mod, name) < 0) {
             goto error;
         }
     }
diff --git a/Python/initconfig.c b/Python/initconfig.c
index 62087fb..7886d09 100644
--- a/Python/initconfig.c
+++ b/Python/initconfig.c
@@ -2909,8 +2909,8 @@ _Py_GetConfigsAsDict(void)
     Py_CLEAR(dict);
 
     /* pre config */
-    PyThreadState *tstate = _PyThreadState_GET();
-    const PyPreConfig *pre_config = &tstate->interp->runtime->preconfig;
+    PyInterpreterState *interp = _PyInterpreterState_GET();
+    const PyPreConfig *pre_config = &interp->runtime->preconfig;
     dict = _PyPreConfig_AsDict(pre_config);
     if (dict == NULL) {
         goto error;
@@ -2921,7 +2921,7 @@ _Py_GetConfigsAsDict(void)
     Py_CLEAR(dict);
 
     /* core config */
-    const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
+    const PyConfig *config = _PyInterpreterState_GetConfig(interp);
     dict = _PyConfig_AsDict(config);
     if (dict == NULL) {
         goto error;
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 */
diff --git a/Python/pystate.c b/Python/pystate.c
index f4fd039..1623bab 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -324,7 +324,7 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
 
     /* Last garbage collection on this interpreter */
     _PyGC_CollectNoFail(tstate);
-    _PyGC_Fini(tstate);
+    _PyGC_Fini(interp);
 
     /* We don't clear sysdict and builtins until the end of this function.
        Because clearing other attributes can execute arbitrary Python code
@@ -1146,7 +1146,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
         HEAD_UNLOCK(runtime);
 
         Py_XDECREF(old_exc);
-        _PyEval_SignalAsyncExc(tstate);
+        _PyEval_SignalAsyncExc(tstate->interp);
         return 1;
     }
     HEAD_UNLOCK(runtime);
@@ -1357,9 +1357,9 @@ _PyGILState_GetInterpreterStateUnsafe(void)
 }
 
 void
-_PyGILState_Fini(PyThreadState *tstate)
+_PyGILState_Fini(PyInterpreterState *interp)
 {
-    struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
+    struct _gilstate_runtime_state *gilstate = &interp->runtime->gilstate;
     PyThread_tss_delete(&gilstate->autoTSSkey);
     gilstate->autoInterpreterState = NULL;
 }
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 33d4e2b..13b9034 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -86,9 +86,9 @@ _PySys_GetObjectId(_Py_Identifier *key)
 }
 
 static PyObject *
-_PySys_GetObject(PyThreadState *tstate, const char *name)
+_PySys_GetObject(PyInterpreterState *interp, const char *name)
 {
-    PyObject *sysdict = tstate->interp->sysdict;
+    PyObject *sysdict = interp->sysdict;
     if (sysdict == NULL) {
         return NULL;
     }
@@ -102,7 +102,7 @@ PySys_GetObject(const char *name)
 
     PyObject *exc_type, *exc_value, *exc_tb;
     _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
-    PyObject *value = _PySys_GetObject(tstate, name);
+    PyObject *value = _PySys_GetObject(tstate->interp, name);
     /* XXX Suppress a new exception if it was raised and restore
      * the old one. */
     _PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
@@ -110,12 +110,12 @@ PySys_GetObject(const char *name)
 }
 
 static int
-sys_set_object(PyThreadState *tstate, PyObject *key, PyObject *v)
+sys_set_object(PyInterpreterState *interp, PyObject *key, PyObject *v)
 {
     if (key == NULL) {
         return -1;
     }
-    PyObject *sd = tstate->interp->sysdict;
+    PyObject *sd = interp->sysdict;
     if (v == NULL) {
         v = _PyDict_Pop(sd, key, Py_None);
         if (v == NULL) {
@@ -130,24 +130,24 @@ sys_set_object(PyThreadState *tstate, PyObject *key, PyObject *v)
 }
 
 static int
-sys_set_object_id(PyThreadState *tstate, _Py_Identifier *key, PyObject *v)
+sys_set_object_id(PyInterpreterState *interp, _Py_Identifier *key, PyObject *v)
 {
-    return sys_set_object(tstate, _PyUnicode_FromId(key), v);
+    return sys_set_object(interp, _PyUnicode_FromId(key), v);
 }
 
 int
 _PySys_SetObjectId(_Py_Identifier *key, PyObject *v)
 {
-    PyThreadState *tstate = _PyThreadState_GET();
-    return sys_set_object_id(tstate, key, v);
+    PyInterpreterState *interp = _PyInterpreterState_GET();
+    return sys_set_object_id(interp, key, v);
 }
 
 static int
-sys_set_object_str(PyThreadState *tstate, const char *name, PyObject *v)
+sys_set_object_str(PyInterpreterState *interp, const char *name, PyObject *v)
 {
     PyObject *key = v ? PyUnicode_InternFromString(name)
                       : PyUnicode_FromString(name);
-    int r = sys_set_object(tstate, key, v);
+    int r = sys_set_object(interp, key, v);
     Py_XDECREF(key);
     return r;
 }
@@ -155,22 +155,21 @@ sys_set_object_str(PyThreadState *tstate, const char *name, PyObject *v)
 int
 PySys_SetObject(const char *name, PyObject *v)
 {
-    PyThreadState *tstate = _PyThreadState_GET();
-    return sys_set_object_str(tstate, name, v);
+    PyInterpreterState *interp = _PyInterpreterState_GET();
+    return sys_set_object_str(interp, name, v);
 }
 
 
 static int
-should_audit(PyInterpreterState *is)
+should_audit(PyInterpreterState *interp)
 {
-    /* tstate->interp cannot be NULL, but test it just in case
-       for extra safety */
-    assert(is != NULL);
-    if (!is) {
+    /* interp must not be NULL, but test it just in case for extra safety */
+    assert(interp != NULL);
+    if (!interp) {
         return 0;
     }
-    return (is->runtime->audit_hook_head
-            || is->audit_hooks
+    return (interp->runtime->audit_hook_head
+            || interp->audit_hooks
             || PyDTrace_AUDIT_ENABLED());
 }
 
@@ -455,15 +454,15 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
         return NULL;
     }
 
-    PyInterpreterState *is = tstate->interp;
-    if (is->audit_hooks == NULL) {
-        is->audit_hooks = PyList_New(0);
-        if (is->audit_hooks == NULL) {
+    PyInterpreterState *interp = tstate->interp;
+    if (interp->audit_hooks == NULL) {
+        interp->audit_hooks = PyList_New(0);
+        if (interp->audit_hooks == NULL) {
             return NULL;
         }
     }
 
-    if (PyList_Append(is->audit_hooks, hook) < 0) {
+    if (PyList_Append(interp->audit_hooks, hook) < 0) {
         return NULL;
     }
 
@@ -1607,8 +1606,8 @@ static PyObject *
 sys_setdlopenflags_impl(PyObject *module, int new_val)
 /*[clinic end generated code: output=ec918b7fe0a37281 input=4c838211e857a77f]*/
 {
-    PyThreadState *tstate = _PyThreadState_GET();
-    tstate->interp->dlopenflags = new_val;
+    PyInterpreterState *interp = _PyInterpreterState_GET();
+    interp->dlopenflags = new_val;
     Py_RETURN_NONE;
 }
 
@@ -1625,8 +1624,8 @@ static PyObject *
 sys_getdlopenflags_impl(PyObject *module)
 /*[clinic end generated code: output=e92cd1bc5005da6e input=dc4ea0899c53b4b6]*/
 {
-    PyThreadState *tstate = _PyThreadState_GET();
-    return PyLong_FromLong(tstate->interp->dlopenflags);
+    PyInterpreterState *interp = _PyInterpreterState_GET();
+    return PyLong_FromLong(interp->dlopenflags);
 }
 
 #endif  /* HAVE_DLOPEN */
@@ -2217,7 +2216,7 @@ get_warnoptions(PyThreadState *tstate)
         if (warnoptions == NULL) {
             return NULL;
         }
-        if (sys_set_object_id(tstate, &PyId_warnoptions, warnoptions)) {
+        if (sys_set_object_id(tstate->interp, &PyId_warnoptions, warnoptions)) {
             Py_DECREF(warnoptions);
             return NULL;
         }
@@ -2310,7 +2309,7 @@ get_xoptions(PyThreadState *tstate)
         if (xoptions == NULL) {
             return NULL;
         }
-        if (sys_set_object_id(tstate, &PyId__xoptions, xoptions)) {
+        if (sys_set_object_id(tstate->interp, &PyId__xoptions, xoptions)) {
             Py_DECREF(xoptions);
             return NULL;
         }
@@ -2511,9 +2510,8 @@ static PyStructSequence_Desc flags_desc = {
 };
 
 static int
-set_flags_from_config(PyObject *flags, PyThreadState *tstate)
+set_flags_from_config(PyInterpreterState *interp, PyObject *flags)
 {
-    PyInterpreterState *interp = tstate->interp;
     const PyPreConfig *preconfig = &interp->runtime->preconfig;
     const PyConfig *config = _PyInterpreterState_GetConfig(interp);
 
@@ -2554,14 +2552,14 @@ set_flags_from_config(PyObject *flags, PyThreadState *tstate)
 
 
 static PyObject*
-make_flags(PyThreadState *tstate)
+make_flags(PyInterpreterState *interp)
 {
     PyObject *flags = PyStructSequence_New(&FlagsType);
     if (flags == NULL) {
         return NULL;
     }
 
-    if (set_flags_from_config(flags, tstate) < 0) {
+    if (set_flags_from_config(interp, flags) < 0) {
         Py_DECREF(flags);
         return NULL;
     }
@@ -2819,7 +2817,7 @@ _PySys_InitCore(PyThreadState *tstate, PyObject *sysdict)
             goto type_init_failed;
         }
     }
-    SET_SYS("flags", make_flags(tstate));
+    SET_SYS("flags", make_flags(tstate->interp));
     /* prevent user from creating new instances */
     FlagsType.tp_init = NULL;
     FlagsType.tp_new = NULL;
@@ -2941,8 +2939,9 @@ sys_create_xoptions_dict(const PyConfig *config)
 int
 _PySys_UpdateConfig(PyThreadState *tstate)
 {
-    PyObject *sysdict = tstate->interp->sysdict;
-    const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
+    PyInterpreterState *interp = tstate->interp;
+    PyObject *sysdict = interp->sysdict;
+    const PyConfig *config = _PyInterpreterState_GetConfig(interp);
     int res;
 
 #define COPY_LIST(KEY, VALUE) \
@@ -2985,11 +2984,11 @@ _PySys_UpdateConfig(PyThreadState *tstate)
 #undef COPY_WSTR
 
     // sys.flags
-    PyObject *flags = _PySys_GetObject(tstate, "flags");  // borrowed ref
+    PyObject *flags = _PySys_GetObject(interp, "flags"); // borrowed ref
     if (flags == NULL) {
         return -1;
     }
-    if (set_flags_from_config(flags, tstate) < 0) {
+    if (set_flags_from_config(interp, flags) < 0) {
         return -1;
     }
 
@@ -3129,8 +3128,8 @@ PySys_SetPath(const wchar_t *path)
     PyObject *v;
     if ((v = makepathobject(path, DELIM)) == NULL)
         Py_FatalError("can't create sys.path");
-    PyThreadState *tstate = _PyThreadState_GET();
-    if (sys_set_object_id(tstate, &PyId_path, v) != 0) {
+    PyInterpreterState *interp = _PyInterpreterState_GET();
+    if (sys_set_object_id(interp, &PyId_path, v) != 0) {
         Py_FatalError("can't assign sys.path");
     }
     Py_DECREF(v);
@@ -3171,7 +3170,7 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
     if (av == NULL) {
         Py_FatalError("no mem for sys.argv");
     }
-    if (sys_set_object_str(tstate, "argv", av) != 0) {
+    if (sys_set_object_str(tstate->interp, "argv", av) != 0) {
         Py_DECREF(av);
         Py_FatalError("can't assign sys.argv");
     }