bpo-40268: Rename _PyInterpreterState_GET_UNSAFE() (GH-19509)

Rename _PyInterpreterState_GET_UNSAFE() to _PyInterpreterState_GET()
for consistency with _PyThreadState_GET() and to have a shorter name
(help to fit into 80 columns).

Add also "assert(tstate != NULL);" to the function.
diff --git a/Include/cpython/pystate.h b/Include/cpython/pystate.h
index 7052228..9b28f66 100644
--- a/Include/cpython/pystate.h
+++ b/Include/cpython/pystate.h
@@ -165,7 +165,7 @@
    This function doesn't check for error. Return NULL before _PyGILState_Init()
    is called and after _PyGILState_Fini() is called.
 
-   See also _PyInterpreterState_Get() and _PyInterpreterState_GET_UNSAFE(). */
+   See also _PyInterpreterState_Get() and _PyInterpreterState_GET(). */
 PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
 
 /* The implementation of sys._current_frames()  Returns a dict mapping
diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h
index 2e78378..c82e8db 100644
--- a/Include/internal/pycore_pystate.h
+++ b/Include/internal/pycore_pystate.h
@@ -78,8 +78,9 @@
 
    See also _PyInterpreterState_Get()
    and _PyGILState_GetInterpreterStateUnsafe(). */
-static inline PyInterpreterState* _PyInterpreterState_GET_UNSAFE(void) {
+static inline PyInterpreterState* _PyInterpreterState_GET(void) {
     PyThreadState *tstate = _PyThreadState_GET();
+    assert(tstate != NULL);
     return tstate->interp;
 }
 
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 492988e..95b023d 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -994,7 +994,7 @@
 {
     assert(errors != NULL && errors != Py_None);
 
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
 #ifndef Py_DEBUG
     /* In release mode, only check in development mode (-X dev) */
     if (!_PyInterpreterState_GetConfig(interp)->dev_mode) {
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index 9853699..5636140 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -1091,7 +1091,7 @@
     boot = PyMem_NEW(struct bootstate, 1);
     if (boot == NULL)
         return PyErr_NoMemory();
-    boot->interp = _PyInterpreterState_GET_UNSAFE();
+    boot->interp = _PyInterpreterState_GET();
     boot->func = func;
     boot->args = args;
     boot->keyw = keyw;
@@ -1213,7 +1213,7 @@
 static PyObject *
 thread__count(PyObject *self, PyObject *Py_UNUSED(ignored))
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     return PyLong_FromLong(interp->num_threads);
 }
 
@@ -1556,7 +1556,7 @@
     PyObject *m, *d, *v;
     double time_max;
     double timeout_max;
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
 
     /* Initialize types: */
     if (PyType_Ready(&localdummytype) < 0)
diff --git a/Modules/main.c b/Modules/main.c
index a9de70b..fb24c6c 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -498,7 +498,7 @@
 static void
 pymain_run_python(int *exitcode)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     /* pymain_run_stdin() modify the config */
     PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
 
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 57f1487..2d603d1 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -451,7 +451,7 @@
 void
 PyOS_BeforeFork(void)
 {
-    run_at_forkers(_PyInterpreterState_GET_UNSAFE()->before_forkers, 1);
+    run_at_forkers(_PyInterpreterState_GET()->before_forkers, 1);
 
     _PyImport_AcquireLock();
 }
@@ -462,7 +462,7 @@
     if (_PyImport_ReleaseLock() <= 0)
         Py_FatalError("failed releasing import lock after fork");
 
-    run_at_forkers(_PyInterpreterState_GET_UNSAFE()->after_forkers_parent, 0);
+    run_at_forkers(_PyInterpreterState_GET()->after_forkers_parent, 0);
 }
 
 void
@@ -476,7 +476,7 @@
     _PyRuntimeState_ReInitThreads(runtime);
     _PyInterpreterState_DeleteExceptMain(runtime);
 
-    run_at_forkers(_PyInterpreterState_GET_UNSAFE()->after_forkers_child, 0);
+    run_at_forkers(_PyInterpreterState_GET()->after_forkers_child, 0);
 }
 
 static int
@@ -6185,7 +6185,7 @@
         check_null_or_callable(after_in_parent, "after_in_parent")) {
         return NULL;
     }
-    interp = _PyInterpreterState_GET_UNSAFE();
+    interp = _PyInterpreterState_GET();
 
     if (register_at_forker(&interp->before_forkers, before)) {
         return NULL;
@@ -6216,7 +6216,7 @@
 {
     pid_t pid;
 
-    if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
+    if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
         PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
         return NULL;
     }
@@ -6251,7 +6251,7 @@
 {
     pid_t pid;
 
-    if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
+    if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
         PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
         return NULL;
     }
@@ -6859,7 +6859,7 @@
     int master_fd = -1;
     pid_t pid;
 
-    if (_PyInterpreterState_GET_UNSAFE() != PyInterpreterState_Main()) {
+    if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
         PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
         return NULL;
     }
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index b089a80..ceb986b 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -1784,7 +1784,7 @@
 int
 PyOS_InterruptOccurred(void)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     if (!_Py_ThreadCanHandleSignals(interp)) {
         return 0;
     }
@@ -1821,7 +1821,7 @@
 int
 _PyOS_IsMainThread(void)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     return _Py_ThreadCanHandleSignals(interp);
 }
 
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 7cb72ce..da04af4 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -6,7 +6,7 @@
 #include "structmember.h"
 #include "pycore_code.h"
 #include "pycore_interp.h"        // PyInterpreterState.co_extra_freefuncs
-#include "pycore_pystate.h"       // _PyInterpreterState_GET_UNSAFE()
+#include "pycore_pystate.h"       // _PyInterpreterState_GET()
 #include "pycore_tupleobject.h"
 #include "clinic/codeobject.c.h"
 
@@ -555,7 +555,7 @@
     co->co_opcache_size = 0;
 
     if (co->co_extra != NULL) {
-        PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+        PyInterpreterState *interp = _PyInterpreterState_GET();
         _PyCodeObjectExtra *co_extra = co->co_extra;
 
         for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
@@ -1074,7 +1074,7 @@
 int
 _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
 
     if (!PyCode_Check(code) || index < 0 ||
             index >= interp->co_extra_user_count) {
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index acb920a..1f419ad 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -165,7 +165,7 @@
 PyObject *
 PyModule_Create2(struct PyModuleDef* module, int module_api_version)
 {
-    if (!_PyImport_IsInitialized(_PyInterpreterState_GET_UNSAFE())) {
+    if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
         PyErr_SetString(PyExc_SystemError,
                         "Python import machinery not initialized");
         return NULL;
@@ -684,7 +684,7 @@
 static PyObject *
 module_repr(PyModuleObject *m)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
 
     return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
 }
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 938df24..28ec8f1 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -436,7 +436,7 @@
         return 0;
     }
 
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
 #ifndef Py_DEBUG
     /* In release mode, only check in development mode (-X dev) */
     if (!_PyInterpreterState_GetConfig(interp)->dev_mode) {
@@ -3615,7 +3615,7 @@
 PyObject *
 PyUnicode_EncodeFSDefault(PyObject *unicode)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     if (interp->fs_codec.utf8) {
         return unicode_encode_utf8(unicode,
                                    interp->fs_codec.error_handler,
@@ -3851,7 +3851,7 @@
 PyObject*
 PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     if (interp->fs_codec.utf8) {
         return unicode_decode_utf8(s, size,
                                    interp->fs_codec.error_handler,
@@ -16009,7 +16009,7 @@
 int
 _PyUnicode_EnableLegacyWindowsFSEncoding(void)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     PyConfig *config = (PyConfig *)_PyInterpreterState_GetConfig(interp);
 
     /* Set the filesystem encoding to mbcs/replace (PEP 529) */
diff --git a/Parser/listnode.c b/Parser/listnode.c
index f53b265..3bcc03e 100644
--- a/Parser/listnode.c
+++ b/Parser/listnode.c
@@ -3,7 +3,7 @@
 
 #include "Python.h"
 #include "pycore_interp.h"   // PyInterpreterState.parser
-#include "pycore_pystate.h"  // _PyInterpreterState_GET_UNSAFE
+#include "pycore_pystate.h"  // _PyInterpreterState_GET
 #include "token.h"
 #include "node.h"
 
@@ -20,7 +20,7 @@
 static void
 listnode(FILE *fp, node *n)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
 
     interp->parser.listnode.level = 0;
     interp->parser.listnode.atbol = 1;
@@ -40,7 +40,7 @@
             list1node(fp, CHILD(n, i));
     }
     else if (ISTERMINAL(TYPE(n))) {
-        interp = _PyInterpreterState_GET_UNSAFE();
+        interp = _PyInterpreterState_GET();
         switch (TYPE(n)) {
         case INDENT:
             interp->parser.listnode.level++;
diff --git a/Python/_warnings.c b/Python/_warnings.c
index d005f12..0236cab 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -212,7 +212,7 @@
            gone, then we can't even use PyImport_GetModule without triggering
            an interpreter abort.
         */
-        if (!_PyInterpreterState_GET_UNSAFE()->modules) {
+        if (!_PyInterpreterState_GET()->modules) {
             return NULL;
         }
         warnings_module = PyImport_GetModule(warnings_str);
@@ -840,7 +840,7 @@
     }
 
     if (f == NULL) {
-        globals = _PyInterpreterState_GET_UNSAFE()->sysdict;
+        globals = _PyInterpreterState_GET()->sysdict;
         *filename = PyUnicode_FromString("sys");
         *lineno = 1;
     }
diff --git a/Python/ceval.c b/Python/ceval.c
index b712920..fc720b4 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -5562,7 +5562,7 @@
 Py_ssize_t
 _PyEval_RequestCodeExtraIndex(freefunc free)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     Py_ssize_t new_index;
 
     if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
diff --git a/Python/codecs.c b/Python/codecs.c
index 6691985..32cc110 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -33,7 +33,7 @@
 
 int PyCodec_Register(PyObject *search_function)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
         goto onError;
     if (search_function == NULL) {
@@ -105,7 +105,7 @@
         return NULL;
     }
 
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     if (interp->codec_search_path == NULL && _PyCodecRegistry_Init()) {
         return NULL;
     }
@@ -188,7 +188,7 @@
     PyObject *v;
     int result;
 
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     if (interp->codec_search_path == NULL) {
         return -1;
     }
@@ -621,7 +621,7 @@
    Return 0 on success, -1 on error */
 int PyCodec_RegisterError(const char *name, PyObject *error)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
         return -1;
     if (!PyCallable_Check(error)) {
@@ -639,7 +639,7 @@
 {
     PyObject *handler = NULL;
 
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
         return NULL;
 
@@ -1491,7 +1491,7 @@
         }
     };
 
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     PyObject *mod;
 
     if (interp->codec_search_path != NULL)
diff --git a/Python/dynload_shlib.c b/Python/dynload_shlib.c
index 223e0d0..082154d 100644
--- a/Python/dynload_shlib.c
+++ b/Python/dynload_shlib.c
@@ -3,7 +3,7 @@
 
 #include "Python.h"
 #include "pycore_interp.h"    // _PyInterpreterState.dlopenflags
-#include "pycore_pystate.h"   // _PyInterpreterState_GET_UNSAFE()
+#include "pycore_pystate.h"   // _PyInterpreterState_GET()
 #include "importdl.h"
 
 #include <sys/types.h>
@@ -95,7 +95,7 @@
         }
     }
 
-    dlopenflags = _PyInterpreterState_GET_UNSAFE()->dlopenflags;
+    dlopenflags = _PyInterpreterState_GET()->dlopenflags;
 
     handle = dlopen(pathname, dlopenflags);
 
diff --git a/Python/import.c b/Python/import.c
index 3bf8fe0..042691d 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -310,7 +310,7 @@
 PyObject *
 PyImport_GetModuleDict(void)
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     if (interp->modules == NULL) {
         Py_FatalError("interpreter has no modules dictionary");
     }
@@ -644,7 +644,7 @@
 PyImport_GetMagicNumber(void)
 {
     long res;
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     PyObject *external, *pyc_magic;
 
     external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external");
@@ -980,7 +980,7 @@
             goto error;
     }
     else if (cpathobj != NULL) {
-        PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+        PyInterpreterState *interp = _PyInterpreterState_GET();
         _Py_IDENTIFIER(_get_sourcefile);
 
         if (interp == NULL) {
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index c2a0781..ac0ea10 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -2355,7 +2355,7 @@
 /* For the atexit module. */
 void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
 {
-    PyInterpreterState *is = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *is = _PyInterpreterState_GET();
 
     /* Guard against API misuse (see bpo-17852) */
     assert(is->pyexitfunc == NULL || is->pyexitfunc == func);
diff --git a/Python/pystate.c b/Python/pystate.c
index 3636dc9..65d46a2 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -661,7 +661,7 @@
 PyState_FindModule(struct PyModuleDef* module)
 {
     Py_ssize_t index = module->m_base.m_index;
-    PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *state = _PyInterpreterState_GET();
     PyObject *res;
     if (module->m_slots) {
         return NULL;
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 522d152..d1165e2 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -347,7 +347,7 @@
     filename_obj = PyUnicode_DecodeFSDefault(filename);
     if (filename_obj == NULL)
         return -1;
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     bootstrap = PyObject_GetAttrString(interp->importlib,
                                        "_bootstrap_external");
     if (bootstrap != NULL) {
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 814e4ab..6b3a1c3 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -847,7 +847,7 @@
 sys_getfilesystemencoding_impl(PyObject *module)
 /*[clinic end generated code: output=1dc4bdbe9be44aa7 input=8475f8649b8c7d8c]*/
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     const PyConfig *config = _PyInterpreterState_GetConfig(interp);
     return PyUnicode_FromWideChar(config->filesystem_encoding, -1);
 }
@@ -862,7 +862,7 @@
 sys_getfilesystemencodeerrors_impl(PyObject *module)
 /*[clinic end generated code: output=ba77b36bbf7c96f5 input=22a1e8365566f1e5]*/
 {
-    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     const PyConfig *config = _PyInterpreterState_GetConfig(interp);
     return PyUnicode_FromWideChar(config->filesystem_errors, -1);
 }
diff --git a/Python/thread.c b/Python/thread.c
index ec6909c..bac0e69 100644
--- a/Python/thread.c
+++ b/Python/thread.c
@@ -92,7 +92,7 @@
 size_t
 PyThread_get_stacksize(void)
 {
-    return _PyInterpreterState_GET_UNSAFE()->pythread_stacksize;
+    return _PyInterpreterState_GET()->pythread_stacksize;
 }
 
 /* Only platforms defining a THREAD_SET_STACKSIZE() macro
diff --git a/Python/thread_nt.h b/Python/thread_nt.h
index e4bd0f7..05b982d 100644
--- a/Python/thread_nt.h
+++ b/Python/thread_nt.h
@@ -359,13 +359,13 @@
 {
     /* set to default */
     if (size == 0) {
-        _PyInterpreterState_GET_UNSAFE()->pythread_stacksize = 0;
+        _PyInterpreterState_GET()->pythread_stacksize = 0;
         return 0;
     }
 
     /* valid range? */
     if (size >= THREAD_MIN_STACKSIZE && size < THREAD_MAX_STACKSIZE) {
-        _PyInterpreterState_GET_UNSAFE()->pythread_stacksize = size;
+        _PyInterpreterState_GET()->pythread_stacksize = size;
         return 0;
     }
 
diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h
index 440d845..cf4e854 100644
--- a/Python/thread_pthread.h
+++ b/Python/thread_pthread.h
@@ -735,7 +735,7 @@
 
     /* set to default */
     if (size == 0) {
-        _PyInterpreterState_GET_UNSAFE()->pythread_stacksize = 0;
+        _PyInterpreterState_GET()->pythread_stacksize = 0;
         return 0;
     }
 
@@ -752,7 +752,7 @@
             rc = pthread_attr_setstacksize(&attrs, size);
             pthread_attr_destroy(&attrs);
             if (rc == 0) {
-                _PyInterpreterState_GET_UNSAFE()->pythread_stacksize = size;
+                _PyInterpreterState_GET()->pythread_stacksize = size;
                 return 0;
             }
         }