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

This commit is contained in:
Benjamin Peterson 2010-10-16 19:20:12 +00:00
parent f76942d6bf
commit bf775542b0
3 changed files with 16 additions and 0 deletions

View File

@ -135,6 +135,14 @@ class AutoFileTests(unittest.TestCase):
def testReadWhenWriting(self): def testReadWhenWriting(self):
self.assertRaises(IOError, self.f.read) 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): def testIssue5677(self):
# Remark: Do not perform more than one test per open file, # Remark: Do not perform more than one test per open file,
# since that does NOT catch the readline error on Windows. # since that does NOT catch the readline error on Windows.

View File

@ -10,6 +10,9 @@ What's New in Python 2.7.1?
Core and Builtins 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 - Issue #9997: Don't let the name "top" have special significance in scope
resolution. resolution.

View File

@ -1849,6 +1849,11 @@ file_writelines(PyFileObject *f, PyObject *seq)
} }
PyList_SetItem(list, j, line); 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) if (j == 0)
break; break;