Issue #20037: Avoid crashes when doing text I/O late at interpreter shutdown.
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index 9866fbe..be0464c 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -539,6 +539,20 @@
 }
 
 
+_PyIO_State *
+_PyIO_get_module_state(void)
+{
+    PyObject *mod = PyState_FindModule(&_PyIO_Module);
+    _PyIO_State *state;
+    if (mod == NULL || (state = IO_MOD_STATE(mod)) == NULL) {
+        PyErr_SetString(PyExc_RuntimeError,
+                        "could not find io module state "
+                        "(interpreter shutdown?)");
+        return NULL;
+    }
+    return state;
+}
+
 PyObject *
 _PyIO_get_locale_module(_PyIO_State *state)
 {