[Python-checkins] cpython (3.2): #10053: Don't close FDs when FileIO.__init__ fails

hynek.schlawack python-checkins at python.org
Thu Jun 21 21:00:23 CEST 2012


http://hg.python.org/cpython/rev/d042bd8625f3
changeset:   77538:d042bd8625f3
branch:      3.2
parent:      77520:e044fa016c85
user:        Hynek Schlawack <hs at ox.cx>
date:        Thu Jun 21 20:20:25 2012 +0200
summary:
  #10053: Don't close FDs when FileIO.__init__ fails

Loosely based on the work by Hirokazu Yamamoto.

files:
  Lib/test/test_fileio.py |  11 +++++++++++
  Misc/NEWS               |   3 +++
  Modules/_io/fileio.c    |  11 +++++------
  3 files changed, 19 insertions(+), 6 deletions(-)


diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -403,6 +403,17 @@
             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.
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -224,6 +224,7 @@
     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 @@
 #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 @@
            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 @@
 
  error:
     ret = -1;
+    if (!fd_is_own)
+        self->fd = -1;
     if (self->fd >= 0)
         internal_close(self);
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list