summaryrefslogtreecommitdiffstats
path: root/Lib/_pyio.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-05-03 16:25:33 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-05-03 16:25:33 (GMT)
commitf7fd8e4c9492defbb32cc142faaf3abeabd00cf8 (patch)
tree8854e7a740532f8b6b746e9e5c4118b95ad758f9 /Lib/_pyio.py
parentc3ce9e357dac697561a8c97e977e49db321571e3 (diff)
downloadcpython-f7fd8e4c9492defbb32cc142faaf3abeabd00cf8.zip
cpython-f7fd8e4c9492defbb32cc142faaf3abeabd00cf8.tar.gz
cpython-f7fd8e4c9492defbb32cc142faaf3abeabd00cf8.tar.bz2
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/_pyio.py')
-rw-r--r--Lib/_pyio.py22
1 files changed, 8 insertions, 14 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 1343f1b..bdffb12 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -327,6 +327,7 @@ class IOBase:
This is not implemented for read-only and non-blocking streams.
"""
+ self._checkClosed()
# XXX Should this return the number of bytes written???
__closed = False
@@ -337,10 +338,7 @@ class IOBase:
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):
@@ -709,14 +707,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):
@@ -1531,11 +1528,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