iterators passed to writelines() can close their files; don't segfault #10125
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index b7de6a1..2647b54 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -1849,6 +1849,11 @@
                 }
                 PyList_SetItem(list, j, line);
             }
+            /* The iterator might have closed the file on us. */
+            if (f->f_fp == NULL) {
+                err_closed();
+                goto error;
+            }
         }
         if (j == 0)
             break;