[Python-checkins] r80723 - in python/branches/release31-maint: Lib/_pyio.py Lib/test/test_io.py Misc/NEWS Modules/_io/bufferedio.c Modules/_io/bytesio.c Modules/_io/iobase.c Modules/_io/textio.c
antoine.pitrou
python-checkins at python.org
Mon May 3 18:58:19 CEST 2010
Author: antoine.pitrou
Date: Mon May 3 18:58:19 2010
New Revision: 80723
Log:
Merged revisions 80722 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r80722 | antoine.pitrou | 2010-05-03 18:48:20 +0200 (lun., 03 mai 2010) | 11 lines
Merged revisions 80720 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r80720 | antoine.pitrou | 2010-05-03 18:25:33 +0200 (lun., 03 mai 2010) | 5 lines
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.
........
................
Modified:
python/branches/release31-maint/ (props changed)
python/branches/release31-maint/Lib/_pyio.py
python/branches/release31-maint/Lib/test/test_io.py
python/branches/release31-maint/Misc/NEWS
python/branches/release31-maint/Modules/_io/bufferedio.c
python/branches/release31-maint/Modules/_io/bytesio.c
python/branches/release31-maint/Modules/_io/iobase.c
python/branches/release31-maint/Modules/_io/textio.c
Modified: python/branches/release31-maint/Lib/_pyio.py
==============================================================================
--- python/branches/release31-maint/Lib/_pyio.py (original)
+++ python/branches/release31-maint/Lib/_pyio.py Mon May 3 18:58:19 2010
@@ -325,6 +325,7 @@
This is not implemented for read-only and non-blocking streams.
"""
+ self._checkClosed()
# XXX Should this return the number of bytes written???
__closed = False
@@ -335,10 +336,7 @@
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) -> None:
@@ -705,14 +703,13 @@
### 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):
@@ -1514,11 +1511,8 @@
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
Modified: python/branches/release31-maint/Lib/test/test_io.py
==============================================================================
--- python/branches/release31-maint/Lib/test/test_io.py (original)
+++ python/branches/release31-maint/Lib/test/test_io.py Mon May 3 18:58:19 2010
@@ -535,6 +535,20 @@
with self.open(zero, "r") as f:
self.assertRaises(OverflowError, f.read)
+ def test_flush_error_on_close(self):
+ f = self.open(support.TESTFN, "wb", buffering=0)
+ def bad_flush():
+ raise IOError()
+ f.flush = bad_flush
+ self.assertRaises(IOError, f.close) # exception not swallowed
+
+ def test_multi_close(self):
+ f = self.open(support.TESTFN, "wb", buffering=0)
+ f.close()
+ f.close()
+ f.close()
+ self.assertRaises(ValueError, f.flush)
+
class CIOTest(IOTest):
pass
@@ -634,6 +648,22 @@
raw.name = b"dummy"
self.assertEqual(repr(b), "<%s name=b'dummy'>" % clsname)
+ def test_flush_error_on_close(self):
+ raw = self.MockRawIO()
+ def bad_flush():
+ raise IOError()
+ raw.flush = bad_flush
+ b = self.tp(raw)
+ self.assertRaises(IOError, b.close) # exception not swallowed
+
+ def test_multi_close(self):
+ raw = self.MockRawIO()
+ b = self.tp(raw)
+ b.close()
+ b.close()
+ b.close()
+ self.assertRaises(ValueError, b.flush)
+
class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
read_mode = "rb"
@@ -2114,6 +2144,20 @@
for n in range(20):
self.assertEquals(content.count("Thread%03d\n" % n), 1)
+ def test_flush_error_on_close(self):
+ txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
+ def bad_flush():
+ raise IOError()
+ txt.flush = bad_flush
+ self.assertRaises(IOError, txt.close) # exception not swallowed
+
+ def test_multi_close(self):
+ txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
+ txt.close()
+ txt.close()
+ txt.close()
+ self.assertRaises(ValueError, txt.flush)
+
class CTextIOWrapperTest(TextIOWrapperTest):
def test_initialization(self):
Modified: python/branches/release31-maint/Misc/NEWS
==============================================================================
--- python/branches/release31-maint/Misc/NEWS (original)
+++ python/branches/release31-maint/Misc/NEWS Mon May 3 18:58:19 2010
@@ -40,6 +40,10 @@
Library
-------
+- 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.
+
- Issue #4687: Fix accuracy of garbage collection runtimes displayed with
gc.DEBUG_STATS.
Modified: python/branches/release31-maint/Modules/_io/bufferedio.c
==============================================================================
--- python/branches/release31-maint/Modules/_io/bufferedio.c (original)
+++ python/branches/release31-maint/Modules/_io/bufferedio.c Mon May 3 18:58:19 2010
@@ -438,11 +438,7 @@
res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
ENTER_BUFFERED(self)
if (res == NULL) {
- /* If flush() fails, just give up */
- if (PyErr_ExceptionMatches(PyExc_IOError))
- PyErr_Clear();
- else
- goto end;
+ goto end;
}
Py_XDECREF(res);
Modified: python/branches/release31-maint/Modules/_io/bytesio.c
==============================================================================
--- python/branches/release31-maint/Modules/_io/bytesio.c (original)
+++ python/branches/release31-maint/Modules/_io/bytesio.c Mon May 3 18:58:19 2010
@@ -169,6 +169,7 @@
static PyObject *
bytesio_flush(bytesio *self)
{
+ CHECK_CLOSED(self);
Py_RETURN_NONE;
}
Modified: python/branches/release31-maint/Modules/_io/iobase.c
==============================================================================
--- python/branches/release31-maint/Modules/_io/iobase.c (original)
+++ python/branches/release31-maint/Modules/_io/iobase.c Mon May 3 18:58:19 2010
@@ -183,11 +183,7 @@
res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
PyObject_SetAttrString(self, "__IOBase_closed", Py_True);
if (res == NULL) {
- /* If flush() fails, just give up */
- if (PyErr_ExceptionMatches(PyExc_IOError))
- PyErr_Clear();
- else
- return NULL;
+ return NULL;
}
Py_XDECREF(res);
Py_RETURN_NONE;
Modified: python/branches/release31-maint/Modules/_io/textio.c
==============================================================================
--- python/branches/release31-maint/Modules/_io/textio.c (original)
+++ python/branches/release31-maint/Modules/_io/textio.c Mon May 3 18:58:19 2010
@@ -2398,16 +2398,30 @@
textiowrapper_close(textio *self, PyObject *args)
{
PyObject *res;
+ int r;
CHECK_INITIALIZED(self);
- res = PyObject_CallMethod((PyObject *)self, "flush", NULL);
- if (res == NULL) {
- /* If flush() fails, just give up */
- PyErr_Clear();
+
+ res = textiowrapper_closed_get(self, NULL);
+ if (res == NULL)
+ return NULL;
+ r = PyObject_IsTrue(res);
+ Py_DECREF(res);
+ if (r < 0)
+ return NULL;
+
+ if (r > 0) {
+ Py_RETURN_NONE; /* stream already closed */
}
- else
- Py_DECREF(res);
+ else {
+ res = PyObject_CallMethod((PyObject *)self, "flush", NULL);
+ if (res == NULL) {
+ return NULL;
+ }
+ else
+ Py_DECREF(res);
- return PyObject_CallMethod(self->buffer, "close", NULL);
+ return PyObject_CallMethod(self->buffer, "close", NULL);
+ }
}
static PyObject *
More information about the Python-checkins
mailing list