summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2012-12-20 17:53:11 (GMT)
committerBenjamin Peterson <benjamin@python.org>2012-12-20 17:53:11 (GMT)
commit68623614f095c8589ff17c004b9a05c537afc01f (patch)
treec42832d39e1919f5352410e9dabe4a7e04f944bd /Lib
parent5ff3f73d94305900c0773e67ebdd9701d856a0fe (diff)
downloadcpython-68623614f095c8589ff17c004b9a05c537afc01f.zip
cpython-68623614f095c8589ff17c004b9a05c537afc01f.tar.gz
cpython-68623614f095c8589ff17c004b9a05c537afc01f.tar.bz2
call close on the underlying stream even if flush raises (closes #16597)
Patch by Serhiy Storchaka.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/_pyio.py12
-rw-r--r--Lib/test/test_io.py28
2 files changed, 36 insertions, 4 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index fa77ec1..9cbb364 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -346,8 +346,10 @@ class IOBase(metaclass=abc.ABCMeta):
This method has no effect if the file is already closed.
"""
if not self.__closed:
- self.flush()
- self.__closed = True
+ try:
+ self.flush()
+ finally:
+ self.__closed = True
def __del__(self):
"""Destructor. Calls close()."""
@@ -1584,8 +1586,10 @@ class TextIOWrapper(TextIOBase):
def close(self):
if self.buffer is not None and not self.closed:
- self.flush()
- self.buffer.close()
+ try:
+ self.flush()
+ finally:
+ self.buffer.close()
@property
def closed(self):
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 555ad74..6931e0a 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -603,6 +603,7 @@ class IOTest(unittest.TestCase):
raise IOError()
f.flush = bad_flush
self.assertRaises(IOError, f.close) # exception not swallowed
+ self.assertTrue(f.closed)
def test_multi_close(self):
f = self.open(support.TESTFN, "wb", buffering=0)
@@ -780,6 +781,22 @@ class CommonBufferedTests:
raw.flush = bad_flush
b = self.tp(raw)
self.assertRaises(IOError, b.close) # exception not swallowed
+ self.assertTrue(b.closed)
+
+ def test_close_error_on_close(self):
+ raw = self.MockRawIO()
+ def bad_flush():
+ raise IOError('flush')
+ def bad_close():
+ raise IOError('close')
+ raw.close = bad_close
+ b = self.tp(raw)
+ b.flush = bad_flush
+ with self.assertRaises(IOError) as err: # exception not swallowed
+ b.close()
+ self.assertEqual(err.exception.args, ('close',))
+ self.assertEqual(err.exception.__context__.args, ('flush',))
+ self.assertFalse(b.closed)
def test_multi_close(self):
raw = self.MockRawIO()
@@ -1296,6 +1313,16 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
with self.assertRaises(TypeError):
self.tp(self.MockRawIO(), 8, 12)
+ def test_write_error_on_close(self):
+ raw = self.MockRawIO()
+ def bad_write(b):
+ raise IOError()
+ raw.write = bad_write
+ b = self.tp(raw)
+ b.write(b'spam')
+ self.assertRaises(IOError, b.close) # exception not swallowed
+ self.assertTrue(b.closed)
+
class CBufferedWriterTest(BufferedWriterTest, SizeofTest):
tp = io.BufferedWriter
@@ -2465,6 +2492,7 @@ class TextIOWrapperTest(unittest.TestCase):
raise IOError()
txt.flush = bad_flush
self.assertRaises(IOError, txt.close) # exception not swallowed
+ self.assertTrue(txt.closed)
def test_multi_close(self):
txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")