diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index fa305da42a2..e9281e33fa2 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -342,6 +342,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 a1b5246f7fc..ce29e40d1c7 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -143,6 +143,9 @@ C-API Library ------- +- Issue #10253: FileIO leaks a file descriptor when trying to open a file + for append that isn't seekable. Patch by Brian Brazil. + - Issue #5027: The standard ``xml`` namespace is now understood by xml.sax.saxutils.XMLGenerator as being bound to http://www.w3.org/XML/1998/namespace. Patch by Troy J. Farrell. diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index f332ee46333..a365c9fe46c 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -364,8 +364,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); }