]> granicus.if.org Git - python/commitdiff
Issue #10253: FileIO leaks a file descriptor when trying to open a file
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 30 Oct 2010 16:19:14 +0000 (16:19 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 30 Oct 2010 16:19:14 +0000 (16:19 +0000)
for append that isn't seekable.  Patch by Brian Brazil.

Lib/test/test_fileio.py
Misc/NEWS
Modules/_io/fileio.c

index 4b48d41f6caed13d7df08dc5c1e74c4bac39cfa3..350ba5d181e4d9840b67a7d63959e2b2ece2a604 100644 (file)
@@ -339,6 +339,7 @@ class OtherFileTests(unittest.TestCase):
         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 827640a5d48529c378a16a7614f7d07cbc19c49d..cadafd02b0864a17393638654cd0dd4d33c4b4f5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -57,6 +57,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #10253: FileIO leaks a file descriptor when trying to open a file
+  for append that isn't seekable.  Patch by Brian Brazil.
+
 - Support context manager protocol for file-like objects returned by
   mailbox ``get_file()`` methods.
 
index 16b98d6f673180911ddbfcb2f2b1acdd04316304..279df34c8d89ba63f29a0a877d92fd74ad479ce8 100644 (file)
@@ -396,8 +396,13 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
            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) {
+            if (closefd) {
+                close(self->fd);
+                self->fd = -1;
+            }
             goto error;
+        }
         Py_DECREF(pos);
     }