]> granicus.if.org Git - python/commitdiff
Issue #7865: The close() method of :mod:`io` objects should not swallow
authorAntoine Pitrou <solipsis@pitrou.net>
Mon, 3 May 2010 16:25:33 +0000 (16:25 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Mon, 3 May 2010 16:25:33 +0000 (16:25 +0000)
exceptions raised by the implicit flush().  Also ensure that calling
close() several times is supported.  Patch by Pascal Chambon.

Lib/_pyio.py
Lib/test/test_io.py
Misc/NEWS
Modules/_io/bufferedio.c
Modules/_io/bytesio.c
Modules/_io/iobase.c
Modules/_io/textio.c

index 1343f1bc7f05f81fb801eaef4e6b4184966b9b1b..bdffb124c711ebbecda835d7c6559726a364cb6f 100644 (file)
@@ -327,6 +327,7 @@ class IOBase:
 
         This is not implemented for read-only and non-blocking streams.
         """
+        self._checkClosed()
         # XXX Should this return the number of bytes written???
 
     __closed = False
@@ -337,10 +338,7 @@ class IOBase:
         This method has no effect if the file is already closed.
         """
         if not self.__closed:
-            try:
-                self.flush()
-            except IOError:
-                pass  # If flush() fails, just give up
+            self.flush()
             self.__closed = True
 
     def __del__(self):
@@ -709,14 +707,13 @@ class _BufferedIOMixin(BufferedIOBase):
     ### Flush and close ###
 
     def flush(self):
+        if self.closed:
+            raise ValueError("flush of closed file")
         self.raw.flush()
 
     def close(self):
-        if not self.closed and self.raw is not None:
-            try:
-                self.flush()
-            except IOError:
-                pass  # If flush() fails, just give up
+        if self.raw is not None and not self.closed:
+            self.flush()
             self.raw.close()
 
     def detach(self):
@@ -1531,11 +1528,8 @@ class TextIOWrapper(TextIOBase):
         self._telling = self._seekable
 
     def close(self):
-        if self.buffer is not None:
-            try:
-                self.flush()
-            except IOError:
-                pass  # If flush() fails, just give up
+        if self.buffer is not None and not self.closed:
+            self.flush()
             self.buffer.close()
 
     @property
index b3feb1b065c90843c7ebb9ab8ce9188625844ad3..2874d79d95d31178e3e666f371ba2c1aa693347a 100644 (file)
@@ -541,6 +541,20 @@ class IOTest(unittest.TestCase):
         with self.open(zero, "r") as f:
             self.assertRaises(OverflowError, f.read)
 
+    def test_flush_error_on_close(self):
+        f = self.open(support.TESTFN, "wb", buffering=0)
+        def bad_flush():
+            raise IOError()
+        f.flush = bad_flush
+        self.assertRaises(IOError, f.close) # exception not swallowed
+
+    def test_multi_close(self):
+        f = self.open(support.TESTFN, "wb", buffering=0)
+        f.close()
+        f.close()
+        f.close()
+        self.assertRaises(ValueError, f.flush)
+
 class CIOTest(IOTest):
     pass
 
@@ -642,6 +656,22 @@ class CommonBufferedTests:
         raw.name = b"dummy"
         self.assertEqual(repr(b), "<%s name='dummy'>" % clsname)
 
+    def test_flush_error_on_close(self):
+        raw = self.MockRawIO()
+        def bad_flush():
+            raise IOError()
+        raw.flush = bad_flush
+        b = self.tp(raw)
+        self.assertRaises(IOError, b.close) # exception not swallowed
+
+    def test_multi_close(self):
+        raw = self.MockRawIO()
+        b = self.tp(raw)
+        b.close()
+        b.close()
+        b.close()
+        self.assertRaises(ValueError, b.flush)
+
 
 class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
     read_mode = "rb"
@@ -2117,6 +2147,20 @@ class TextIOWrapperTest(unittest.TestCase):
             for n in range(20):
                 self.assertEquals(content.count("Thread%03d\n" % n), 1)
 
+    def test_flush_error_on_close(self):
+        txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
+        def bad_flush():
+            raise IOError()
+        txt.flush = bad_flush
+        self.assertRaises(IOError, txt.close) # exception not swallowed
+
+    def test_multi_close(self):
+        txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
+        txt.close()
+        txt.close()
+        txt.close()
+        self.assertRaises(ValueError, txt.flush)
+
 class CTextIOWrapperTest(TextIOWrapperTest):
 
     def test_initialization(self):
index e024d60a9b07d72e9ee81f0bfb4e497db7b1023f..12e13e9ba0eca07cffd25f59da1046ff886b73a6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -31,6 +31,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #7865: The close() method of :mod:`io` objects should not swallow
+  exceptions raised by the implicit flush().  Also ensure that calling
+  close() several times is supported.  Patch by Pascal Chambon.
+
 - Issue #8576: logging updated to remove usage of find_unused_port().
 
 - Issue #4687: Fix accuracy of garbage collection runtimes displayed with
index ba937eda837171ada7ae51f13ce2b53d419356df..feee9be0087de9bdb1ea8e920c7ba239d4a482f0 100644 (file)
@@ -440,11 +440,7 @@ buffered_close(buffered *self, PyObject *args)
     res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
     ENTER_BUFFERED(self)
     if (res == NULL) {
-        /* If flush() fails, just give up */
-        if (PyErr_ExceptionMatches(PyExc_IOError))
-            PyErr_Clear();
-        else
-            goto end;
+        goto end;
     }
     Py_XDECREF(res);
 
index 25ff2471b6b896905b7d944ff87c64c5ca65060b..a2943327c73f7c0302924a6b566c3c1f7fcbaf81 100644 (file)
@@ -169,6 +169,7 @@ PyDoc_STRVAR(flush_doc,
 static PyObject *
 bytesio_flush(bytesio *self)
 {
+    CHECK_CLOSED(self);
     Py_RETURN_NONE;
 }
 
index 33448479df6a1d8c01b50a581a4a3554704f38c2..353056251465d12edaaec969b5d4c6ef8382dd71 100644 (file)
@@ -183,11 +183,7 @@ iobase_close(PyObject *self, PyObject *args)
     res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
     PyObject_SetAttrString(self, "__IOBase_closed", Py_True);
     if (res == NULL) {
-        /* If flush() fails, just give up */
-        if (PyErr_ExceptionMatches(PyExc_IOError))
-            PyErr_Clear();
-        else
-            return NULL;
+        return NULL;
     }
     Py_XDECREF(res);
     Py_RETURN_NONE;
index 90a4c09f7a40c9bd1b1636a5e5fa079540df791b..c5d7b85a47503ea068da6432a6a6a890f54db75e 100644 (file)
@@ -2405,16 +2405,30 @@ static PyObject *
 textiowrapper_close(textio *self, PyObject *args)
 {
     PyObject *res;
+    int r;
     CHECK_INITIALIZED(self);
-    res = PyObject_CallMethod((PyObject *)self, "flush", NULL);
-    if (res == NULL) {
-        /* If flush() fails, just give up */
-        PyErr_Clear();
+
+    res = textiowrapper_closed_get(self, NULL);
+    if (res == NULL)
+        return NULL;
+    r = PyObject_IsTrue(res);
+    Py_DECREF(res);
+    if (r < 0)
+        return NULL;
+    
+    if (r > 0) {
+        Py_RETURN_NONE; /* stream already closed */
     }
-    else
-        Py_DECREF(res);
+    else {
+        res = PyObject_CallMethod((PyObject *)self, "flush", NULL);
+        if (res == NULL) {
+            return NULL;
+        }
+        else
+            Py_DECREF(res);
 
-    return PyObject_CallMethod(self->buffer, "close", NULL);
+        return PyObject_CallMethod(self->buffer, "close", NULL);
+    }
 }
 
 static PyObject *