bpo-31404: Revert "remove modules from Py_InterpreterState (#1638)" (#3565)

PR #1638, for bpo-28411, causes problems in some (very) edge cases. Until that gets sorted out, we're reverting the merge. PR #3506, a fix on top of #1638, is also getting reverted.
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index bcbe4ac..3165b4e 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -6425,7 +6425,9 @@
 /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
 {
     PyObject *global;
+    PyObject *modules_dict;
     PyObject *module;
+    _Py_IDENTIFIER(modules);
 
     /* Try to map the old names used in Python 2.x to the new ones used in
        Python 3.x.  We do this only with old pickle protocols and when the
@@ -6482,7 +6484,13 @@
         }
     }
 
-    module = PyImport_GetModule(module_name);
+    modules_dict = _PySys_GetObjectId(&PyId_modules);
+    if (modules_dict == NULL) {
+        PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
+        return NULL;
+    }
+
+    module = PyDict_GetItemWithError(modules_dict, module_name);
     if (module == NULL) {
         if (PyErr_Occurred())
             return NULL;
@@ -6490,11 +6498,11 @@
         if (module == NULL)
             return NULL;
         global = getattribute(module, global_name, self->proto >= 4);
+        Py_DECREF(module);
     }
     else {
         global = getattribute(module, global_name, self->proto >= 4);
     }
-    Py_DECREF(module);
     return global;
 }
 
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index c8a01d4..d9cfa3e 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -1643,6 +1643,7 @@
     PyObject *errors_module;
     PyObject *modelmod_name;
     PyObject *model_module;
+    PyObject *sys_modules;
     PyObject *tmpnum, *tmpstr;
     PyObject *codes_dict;
     PyObject *rev_codes_dict;
@@ -1692,6 +1693,11 @@
     */
     PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
 
+    sys_modules = PySys_GetObject("modules");
+    if (sys_modules == NULL) {
+        Py_DECREF(m);
+        return NULL;
+    }
     d = PyModule_GetDict(m);
     if (d == NULL) {
         Py_DECREF(m);
@@ -1701,7 +1707,7 @@
     if (errors_module == NULL) {
         errors_module = PyModule_New(MODULE_NAME ".errors");
         if (errors_module != NULL) {
-            _PyImport_SetModule(errmod_name, errors_module);
+            PyDict_SetItem(sys_modules, errmod_name, errors_module);
             /* gives away the reference to errors_module */
             PyModule_AddObject(m, "errors", errors_module);
         }
@@ -1711,7 +1717,7 @@
     if (model_module == NULL) {
         model_module = PyModule_New(MODULE_NAME ".model");
         if (model_module != NULL) {
-            _PyImport_SetModule(modelmod_name, model_module);
+            PyDict_SetItem(sys_modules, modelmod_name, model_module);
             /* gives away the reference to model_module */
             PyModule_AddObject(m, "model", model_module);
         }