[Python-checkins] r85574 - in python/branches/release27-maint: Lib/test/test_file2k.py Misc/NEWS Objects/fileobject.c

benjamin.peterson python-checkins at python.org
Sat Oct 16 21:20:12 CEST 2010


Author: benjamin.peterson
Date: Sat Oct 16 21:20:12 2010
New Revision: 85574

Log:
iterators passed to writelines() can close their files; don't segfault #10125

Modified:
   python/branches/release27-maint/Lib/test/test_file2k.py
   python/branches/release27-maint/Misc/NEWS
   python/branches/release27-maint/Objects/fileobject.c

Modified: python/branches/release27-maint/Lib/test/test_file2k.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_file2k.py	(original)
+++ python/branches/release27-maint/Lib/test/test_file2k.py	Sat Oct 16 21:20:12 2010
@@ -135,6 +135,14 @@
     def testReadWhenWriting(self):
         self.assertRaises(IOError, self.f.read)
 
+    def testNastyWritelinesGenerator(self):
+        def nasty():
+            for i in range(5):
+                if i == 3:
+                    self.f.close()
+                yield str(i)
+        self.assertRaises(ValueError, self.f.writelines, nasty())
+
     def testIssue5677(self):
         # Remark: Do not perform more than one test per open file,
         # since that does NOT catch the readline error on Windows.

Modified: python/branches/release27-maint/Misc/NEWS
==============================================================================
--- python/branches/release27-maint/Misc/NEWS	(original)
+++ python/branches/release27-maint/Misc/NEWS	Sat Oct 16 21:20:12 2010
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #10125: Don't segfault when the iterator passed to ``file.writelines()``
+  closes the file.
+
 - Issue #9997: Don't let the name "top" have special significance in scope
   resolution.
 

Modified: python/branches/release27-maint/Objects/fileobject.c
==============================================================================
--- python/branches/release27-maint/Objects/fileobject.c	(original)
+++ python/branches/release27-maint/Objects/fileobject.c	Sat Oct 16 21:20:12 2010
@@ -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;


More information about the Python-checkins mailing list