summaryrefslogtreecommitdiffstats
path: root/Lib/_pyio.py
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/_pyio.py
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/_pyio.py')
-rw-r--r--Lib/_pyio.py12
1 files changed, 8 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):