Merged revisions 80720 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r80720 | antoine.pitrou | 2010-05-03 18:25:33 +0200 (lun., 03 mai 2010) | 5 lines

  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.
........
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 38c58c3..c732b1f 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -313,6 +313,20 @@
             file = io.open(f.fileno(), "r", closefd=False)
             self.assertEqual(file.buffer.raw.closefd, False)
 
+    def test_flush_error_on_close(self):
+        f = io.open(test_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 = io.open(test_support.TESTFN, "wb", buffering=0)
+        f.close()
+        f.close()
+        f.close()
+        self.assertRaises(ValueError, f.flush)
+
 
 class MemorySeekTestMixin:
 
@@ -565,6 +579,22 @@
         finally:
             test_support.unlink(test_support.TESTFN)
 
+    def test_flush_error_on_close(self):
+        raw = MockRawIO()
+        def bad_flush():
+            raise IOError()
+        raw.flush = bad_flush
+        b = io.BufferedWriter(raw)
+        self.assertRaises(IOError, b.close) # exception not swallowed
+
+    def test_multi_close(self):
+        raw = MockRawIO()
+        b = io.BufferedWriter(raw)
+        b.close()
+        b.close()
+        b.close()
+        self.assertRaises(ValueError, b.flush)
+
 
 class BufferedRWPairTest(unittest.TestCase):
 
@@ -1295,6 +1325,20 @@
         decoder = io.IncrementalNewlineDecoder(decoder, translate=True)
         self.check_newline_decoder_utf8(decoder)
 
+    def test_flush_error_on_close(self):
+        txt = io.TextIOWrapper(io.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 = io.TextIOWrapper(io.BytesIO(self.testdata), encoding="ascii")
+        txt.close()
+        txt.close()
+        txt.close()
+        self.assertRaises(ValueError, txt.flush)
+
 
 # XXX Tests for open()