Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d2b51b

Browse files
committed
Issue #10253: FileIO leaks a file descriptor when trying to open a file
for append that isn't seekable. Patch by Brian Brazil.
1 parent daf83ac commit 8d2b51b

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

Lib/test/test_fileio.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ def testTruncate(self):
339339
f.truncate(15)
340340
self.assertEqual(f.tell(), 5)
341341
self.assertEqual(f.seek(0, os.SEEK_END), 15)
342+
f.close()
342343

343344
def testTruncateOnWindows(self):
344345
def bug801631():

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ Core and Builtins
5757
Library
5858
-------
5959

60+
- Issue #10253: FileIO leaks a file descriptor when trying to open a file
61+
for append that isn't seekable. Patch by Brian Brazil.
62+
6063
- Support context manager protocol for file-like objects returned by
6164
mailbox ``get_file()`` methods.
6265

Modules/_io/fileio.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,13 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
396396
end of file (otherwise, it might be done only on the
397397
first write()). */
398398
PyObject *pos = portable_lseek(self->fd, NULL, 2);
399-
if (pos == NULL)
399+
if (pos == NULL) {
400+
if (closefd) {
401+
close(self->fd);
402+
self->fd = -1;
403+
}
400404
goto error;
405+
}
401406
Py_DECREF(pos);
402407
}
403408

0 commit comments

Comments
 (0)