#10053: Don't close FDs when FileIO.__init__ fails
authorHynek Schlawack <hs@ox.cx>
Thu, 21 Jun 2012 18:20:25 +0000 (20:20 +0200)
committerHynek Schlawack <hs@ox.cx>
Thu, 21 Jun 2012 18:20:25 +0000 (20:20 +0200)
Loosely based on the work by Hirokazu Yamamoto.

Lib/test/test_fileio.py
Misc/NEWS
Modules/_io/fileio.c

index 3588fb449b401da83e59d2d94a87a73b11c6ec01..173ec2543752f4376a64070f1bbd76a092f38385 100644 (file)
@@ -403,6 +403,17 @@ class OtherFileTests(unittest.TestCase):
             self.assertRaises(ValueError, _FileIO, "/some/invalid/name", "rt")
             self.assertEqual(w.warnings, [])
 
+    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.
index 330e51a53a9197987cb420320b5008c1236b468b..e59ed37d6119b05a15b137239a49a13aaeb00226 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.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.
 
index c51d69798bdbf073b0c0bf7daefcdb09a4a58ad0..6feca2137f4e010f36c642e5282ec7ef3f53a2b6 100644 (file)
@@ -224,6 +224,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) {
@@ -362,6 +363,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)
@@ -388,13 +390,8 @@ 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);
     }
 
@@ -402,6 +399,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
 
  error:
     ret = -1;
+    if (!fd_is_own)
+        self->fd = -1;
     if (self->fd >= 0)
         internal_close(self);