mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
call close on the underlying stream even if flush raises (closes #16597)
Patch by Serhiy Storchaka.
This commit is contained in:
parent
5ff3f73d94
commit
68623614f0
5 changed files with 80 additions and 13 deletions
12
Lib/_pyio.py
12
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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue