call close on the underlying stream even if flush raises (#16597)

This commit is contained in:
Benjamin Peterson 2012-12-20 12:24:10 -06:00
parent bacf1bf355
commit a2d6d7121e
4 changed files with 68 additions and 13 deletions

View file

@ -340,8 +340,10 @@ class IOBase:
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()."""
@ -1568,8 +1570,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):