From 9bd4bf2a3d0426ee3830e89854ecf4bea03ac830 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Thu, 21 Jun 2012 19:45:19 +0200 Subject: [PATCH] #10053: Don't close FDs when FileIO.__init__ fails Loosely based on the work by Hirokazu Yamamoto. --- Lib/test/test_fileio.py | 11 +++++++++++ Misc/NEWS | 3 +++ Modules/_io/fileio.c | 12 ++++++------ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py index 9688ffc08a0..4d13ce5bf2c 100644 --- a/Lib/test/test_fileio.py +++ b/Lib/test/test_fileio.py @@ -421,6 +421,17 @@ class OtherFileTests(unittest.TestCase): 'IOError: [Errno 2] No such file or directory' not in out): self.fail('Bad output: %r' % out) + def testUnclosedFDOnException(self): + class MyException(Exception): pass + class MyFileIO(_FileIO): + def __setattr__(self, name, value): + if name == "name": + raise MyException("blocked setting name") + return super(MyFileIO, self).__setattr__(name, value) + fd = os.open(__file__, os.O_RDONLY) + self.assertRaises(MyException, MyFileIO, fd) + os.close(fd) # should not raise OSError(EBADF) + def test_main(): # Historically, these tests have been sloppy about removing TESTFN. # So get rid of it no matter what. diff --git a/Misc/NEWS b/Misc/NEWS index 11fb95fc2a3..4ce3e21e85f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -9,6 +9,9 @@ What's New in Python 2.7.4 Core and Builtins ----------------- +- Issue #10053: Don't close FDs when FileIO.__init__ fails. Loosely based on + the work by Hirokazu Yamamoto. + - Issue #14775: Fix a potential quadratic dict build-up due to the garbage collector repeatedly trying to untrack dicts. diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index d1941dfa74e..e23fc6ed17d 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -195,6 +195,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) int flags = 0; int fd = -1; int closefd = 1; + int fd_is_own = 0; assert(PyFileIO_Check(oself)); if (self->fd >= 0) { @@ -345,6 +346,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) #endif self->fd = open(name, flags, 0666); Py_END_ALLOW_THREADS + fd_is_own = 1; if (self->fd < 0) { #ifdef MS_WINDOWS if (widename != NULL) @@ -366,19 +368,17 @@ 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 (closefd) { - close(self->fd); - self->fd = -1; - } + if (pos == NULL) goto error; - } Py_DECREF(pos); } goto done; error: + if (!fd_is_own) + self->fd = -1; + ret = -1; done: