From: Antoine Pitrou Date: Sat, 30 Oct 2010 16:19:14 +0000 (+0000) Subject: Issue #10253: FileIO leaks a file descriptor when trying to open a file X-Git-Tag: v3.2a4~254 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8d2b51b46a1ba0e09f7ee8cb78c848768a675604;p=python Issue #10253: FileIO leaks a file descriptor when trying to open a file for append that isn't seekable. Patch by Brian Brazil. --- diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 4b48d41f6c..350ba5d181 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -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(): diff --git a/Misc/NEWS b/Misc/NEWS index 827640a5d4..cadafd02b0 100644 --- 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. diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 16b98d6f67..279df34c8d 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -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); }