correctly handle invalid operations on streams (like writing on a non-writable one)
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 8d2a686..b78256e 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -1228,6 +1228,11 @@
 
     CHECK_CLOSED(self);
 
+    if (self->encoder == NULL) {
+        PyErr_SetString(PyExc_IOError, "not writable");
+        return NULL;
+    }
+
     Py_INCREF(text);
 
     textlen = PyUnicode_GetSize(text);
@@ -1363,7 +1368,7 @@
      */
 
     if (self->decoder == NULL) {
-        PyErr_SetString(PyExc_ValueError, "no decoder");
+        PyErr_SetString(PyExc_IOError, "not readable");
         return -1;
     }