bpo-39947: Use _PyInterpreterState_GET_UNSAFE() (GH-18978)

Replace _PyInterpreterState_Get() function call with
_PyInterpreterState_GET_UNSAFE() macro which is more efficient but
don't check if tstate or interp is NULL.

_Py_GetConfigsAsDict() now uses _PyThreadState_GET().
diff --git a/Python/codecs.c b/Python/codecs.c
index e5bcdb0..bbbf774 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -32,7 +32,7 @@
 
 int PyCodec_Register(PyObject *search_function)
 {
-    PyInterpreterState *interp = _PyInterpreterState_Get();
+    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
     if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
         goto onError;
     if (search_function == NULL) {
@@ -187,7 +187,7 @@
     PyObject *v;
     int result;
 
-    PyInterpreterState *interp = _PyInterpreterState_Get();
+    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
     if (interp->codec_search_path == NULL) {
         return -1;
     }
@@ -620,7 +620,7 @@
    Return 0 on success, -1 on error */
 int PyCodec_RegisterError(const char *name, PyObject *error)
 {
-    PyInterpreterState *interp = _PyInterpreterState_Get();
+    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
     if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
         return -1;
     if (!PyCallable_Check(error)) {
@@ -1492,7 +1492,7 @@
         }
     };
 
-    PyInterpreterState *interp = _PyInterpreterState_Get();
+    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
     PyObject *mod;
 
     if (interp->codec_search_path != NULL)
diff --git a/Python/dynload_shlib.c b/Python/dynload_shlib.c
index c51f97a..3603871 100644
--- a/Python/dynload_shlib.c
+++ b/Python/dynload_shlib.c
@@ -2,7 +2,7 @@
 /* Support for dynamic loading of extension modules */
 
 #include "Python.h"
-#include "pycore_pystate.h"
+#include "pycore_pystate.h"   // _PyInterpreterState_GET_UNSAFE()
 #include "importdl.h"
 
 #include <sys/types.h>
@@ -94,7 +94,7 @@
         }
     }
 
-    dlopenflags = _PyInterpreterState_Get()->dlopenflags;
+    dlopenflags = _PyInterpreterState_GET_UNSAFE()->dlopenflags;
 
     handle = dlopen(pathname, dlopenflags);
 
diff --git a/Python/import.c b/Python/import.c
index c4a19bc..936ad1f 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -642,7 +642,7 @@
 PyImport_GetMagicNumber(void)
 {
     long res;
-    PyInterpreterState *interp = _PyInterpreterState_Get();
+    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
     PyObject *external, *pyc_magic;
 
     external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external");
@@ -2412,7 +2412,7 @@
         goto failure;
     }
 
-    const wchar_t *mode = _PyInterpreterState_Get()->config.check_hash_pycs_mode;
+    const wchar_t *mode = _PyInterpreterState_GET_UNSAFE()->config.check_hash_pycs_mode;
     PyObject *pyc_mode = PyUnicode_FromWideChar(mode, -1);
     if (pyc_mode == NULL) {
         goto failure;
diff --git a/Python/initconfig.c b/Python/initconfig.c
index 22232ad..19070d2 100644
--- a/Python/initconfig.c
+++ b/Python/initconfig.c
@@ -2572,8 +2572,8 @@
     Py_CLEAR(dict);
 
     /* pre config */
-    PyInterpreterState *interp = _PyInterpreterState_Get();
-    const PyPreConfig *pre_config = &_PyRuntime.preconfig;
+    PyThreadState *tstate = _PyThreadState_GET();
+    const PyPreConfig *pre_config = &tstate->interp->runtime->preconfig;
     dict = _PyPreConfig_AsDict(pre_config);
     if (dict == NULL) {
         goto error;
@@ -2584,7 +2584,7 @@
     Py_CLEAR(dict);
 
     /* core config */
-    const PyConfig *config = &interp->config;
+    const PyConfig *config = &tstate->interp->config;
     dict = config_as_dict(config);
     if (dict == NULL) {
         goto error;
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index f4ded2e..76bc48d 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -95,7 +95,7 @@
     PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
     int nomem_count = 0;
 #ifdef Py_REF_DEBUG
-    int show_ref_count = _PyInterpreterState_Get()->config.show_ref_count;
+    int show_ref_count = _PyInterpreterState_GET_UNSAFE()->config.show_ref_count;
 #endif
 
     filename = PyUnicode_DecodeFSDefault(filename_str);
@@ -346,7 +346,7 @@
     filename_obj = PyUnicode_DecodeFSDefault(filename);
     if (filename_obj == NULL)
         return -1;
-    PyInterpreterState *interp = _PyInterpreterState_Get();
+    PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
     bootstrap = PyObject_GetAttrString(interp->importlib,
                                        "_bootstrap_external");
     if (bootstrap != NULL) {
@@ -1117,7 +1117,7 @@
 
     /* Set globals['__builtins__'] if it doesn't exist */
     if (globals != NULL && PyDict_GetItemString(globals, "__builtins__") == NULL) {
-        PyInterpreterState *interp = _PyInterpreterState_Get();
+        PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
         if (PyDict_SetItemString(globals, "__builtins__", interp->builtins) < 0) {
             return NULL;
         }
diff --git a/Python/thread.c b/Python/thread.c
index c36ce6f..ec6909c 100644
--- a/Python/thread.c
+++ b/Python/thread.c
@@ -92,7 +92,7 @@
 size_t
 PyThread_get_stacksize(void)
 {
-    return _PyInterpreterState_Get()->pythread_stacksize;
+    return _PyInterpreterState_GET_UNSAFE()->pythread_stacksize;
 }
 
 /* Only platforms defining a THREAD_SET_STACKSIZE() macro