summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-05-03 16:48:20 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-05-03 16:48:20 (GMT)
commit6be8876623ffab3f2d1e4af46e7ce169d84b35a6 (patch)
tree66fdad672568ed51cbc8e1d598a104d4037381d2 /Lib
parentfb7b66821678701c1847b2e950ddb6f061da27a2 (diff)
downloadcpython-6be8876623ffab3f2d1e4af46e7ce169d84b35a6.zip
cpython-6be8876623ffab3f2d1e4af46e7ce169d84b35a6.tar.gz
cpython-6be8876623ffab3f2d1e4af46e7ce169d84b35a6.tar.bz2
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. ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/_pyio.py22
-rw-r--r--Lib/test/test_io.py44
2 files changed, 52 insertions, 14 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 1fbefd2..66f990d 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -322,6 +322,7 @@ class IOBase(metaclass=abc.ABCMeta):
This is not implemented for read-only and non-blocking streams.
"""
+ self._checkClosed()
# XXX Should this return the number of bytes written???
__closed = False
@@ -332,10 +333,7 @@ class IOBase(metaclass=abc.ABCMeta):
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) -> None:
@@ -702,14 +700,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):
@@ -1521,11 +1518,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
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index fa96f9a..aa1679b 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -536,6 +536,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
@@ -635,6 +649,22 @@ class CommonBufferedTests:
raw.name = b"dummy"
self.assertEqual(repr(b), "<%s name=b'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"
@@ -2107,6 +2137,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):