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

Skip to content

Commit 9ed8b4e

Browse files
committed
#10053: Don't close FDs when FileIO.__init__ fails
Loosely based on the work by Hirokazu Yamamoto.
1 parent 103e811 commit 9ed8b4e

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

Lib/test/test_fileio.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,17 @@ def testWarnings(self):
403403
self.assertRaises(ValueError, _FileIO, "/some/invalid/name", "rt")
404404
self.assertEqual(w.warnings, [])
405405

406+
def testUnclosedFDOnException(self):
407+
class MyException(Exception): pass
408+
class MyFileIO(_FileIO):
409+
def __setattr__(self, name, value):
410+
if name == "name":
411+
raise MyException("blocked setting name")
412+
return super(MyFileIO, self).__setattr__(name, value)
413+
fd = os.open(__file__, os.O_RDONLY)
414+
self.assertRaises(MyException, MyFileIO, fd)
415+
os.close(fd) # should not raise OSError(EBADF)
416+
406417

407418
def test_main():
408419
# Historically, these tests have been sloppy about removing TESTFN.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #10053: Don't close FDs when FileIO.__init__ fails. Loosely based on
14+
the work by Hirokazu Yamamoto.
15+
1316
- Issue #14775: Fix a potential quadratic dict build-up due to the garbage
1417
collector repeatedly trying to untrack dicts.
1518

Modules/_io/fileio.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
224224
int flags = 0;
225225
int fd = -1;
226226
int closefd = 1;
227+
int fd_is_own = 0;
227228

228229
assert(PyFileIO_Check(oself));
229230
if (self->fd >= 0) {
@@ -362,6 +363,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
362363
#endif
363364
self->fd = open(name, flags, 0666);
364365
Py_END_ALLOW_THREADS
366+
fd_is_own = 1;
365367
if (self->fd < 0) {
366368
#ifdef MS_WINDOWS
367369
if (widename != NULL)
@@ -388,20 +390,17 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
388390
end of file (otherwise, it might be done only on the
389391
first write()). */
390392
PyObject *pos = portable_lseek(self->fd, NULL, 2);
391-
if (pos == NULL) {
392-
if (closefd) {
393-
close(self->fd);
394-
self->fd = -1;
395-
}
393+
if (pos == NULL)
396394
goto error;
397-
}
398395
Py_DECREF(pos);
399396
}
400397

401398
goto done;
402399

403400
error:
404401
ret = -1;
402+
if (!fd_is_own)
403+
self->fd = -1;
405404
if (self->fd >= 0)
406405
internal_close(self);
407406

0 commit comments

Comments
 (0)