Issue #20037: Avoid crashes when doing text I/O late at interpreter shutdown.
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index a04b48d..34c2bb9 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -91,7 +91,9 @@
 static PyObject *
 bufferediobase_unsupported(const char *message)
 {
-    PyErr_SetString(IO_STATE->unsupported_operation, message);
+    _PyIO_State *state = IO_STATE();
+    if (state != NULL)
+        PyErr_SetString(state->unsupported_operation, message);
     return NULL;
 }