Index: Lib/test/test_fileio.py =================================================================== --- Lib/test/test_fileio.py (revision 85979) +++ Lib/test/test_fileio.py (working copy) @@ -339,6 +339,7 @@ f.truncate(15) self.assertEqual(f.tell(), 5) self.assertEqual(f.seek(0, os.SEEK_END), 15) + f.close() def testTruncateOnWindows(self): def bug801631(): Index: Modules/_io/fileio.c =================================================================== --- Modules/_io/fileio.c (revision 85979) +++ Modules/_io/fileio.c (working copy) @@ -396,8 +396,11 @@ end of file (otherwise, it might be done only on the first write()). */ PyObject *pos = portable_lseek(self->fd, NULL, 2); - if (pos == NULL) + if (pos == NULL) { + close(self->fd); + self->fd = -1; goto error; + } Py_DECREF(pos); }