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. ........
This commit is contained in:
parent
fb7b668216
commit
6be8876623
22
Lib/_pyio.py
22
Lib/_pyio.py
|
@ -322,6 +322,7 @@ class IOBase(metaclass=abc.ABCMeta):
|
||||||
|
|
||||||
This is not implemented for read-only and non-blocking streams.
|
This is not implemented for read-only and non-blocking streams.
|
||||||
"""
|
"""
|
||||||
|
self._checkClosed()
|
||||||
# XXX Should this return the number of bytes written???
|
# XXX Should this return the number of bytes written???
|
||||||
|
|
||||||
__closed = False
|
__closed = False
|
||||||
|
@ -332,10 +333,7 @@ class IOBase(metaclass=abc.ABCMeta):
|
||||||
This method has no effect if the file is already closed.
|
This method has no effect if the file is already closed.
|
||||||
"""
|
"""
|
||||||
if not self.__closed:
|
if not self.__closed:
|
||||||
try:
|
self.flush()
|
||||||
self.flush()
|
|
||||||
except IOError:
|
|
||||||
pass # If flush() fails, just give up
|
|
||||||
self.__closed = True
|
self.__closed = True
|
||||||
|
|
||||||
def __del__(self) -> None:
|
def __del__(self) -> None:
|
||||||
|
@ -702,14 +700,13 @@ class _BufferedIOMixin(BufferedIOBase):
|
||||||
### Flush and close ###
|
### Flush and close ###
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
|
if self.closed:
|
||||||
|
raise ValueError("flush of closed file")
|
||||||
self.raw.flush()
|
self.raw.flush()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
if not self.closed and self.raw is not None:
|
if self.raw is not None and not self.closed:
|
||||||
try:
|
self.flush()
|
||||||
self.flush()
|
|
||||||
except IOError:
|
|
||||||
pass # If flush() fails, just give up
|
|
||||||
self.raw.close()
|
self.raw.close()
|
||||||
|
|
||||||
def detach(self):
|
def detach(self):
|
||||||
|
@ -1521,11 +1518,8 @@ class TextIOWrapper(TextIOBase):
|
||||||
self._telling = self._seekable
|
self._telling = self._seekable
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
if self.buffer is not None:
|
if self.buffer is not None and not self.closed:
|
||||||
try:
|
self.flush()
|
||||||
self.flush()
|
|
||||||
except IOError:
|
|
||||||
pass # If flush() fails, just give up
|
|
||||||
self.buffer.close()
|
self.buffer.close()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -536,6 +536,20 @@ class IOTest(unittest.TestCase):
|
||||||
with self.open(zero, "r") as f:
|
with self.open(zero, "r") as f:
|
||||||
self.assertRaises(OverflowError, f.read)
|
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):
|
class CIOTest(IOTest):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -635,6 +649,22 @@ class CommonBufferedTests:
|
||||||
raw.name = b"dummy"
|
raw.name = b"dummy"
|
||||||
self.assertEqual(repr(b), "<%s name=b'dummy'>" % clsname)
|
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):
|
class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
|
||||||
read_mode = "rb"
|
read_mode = "rb"
|
||||||
|
@ -2107,6 +2137,20 @@ class TextIOWrapperTest(unittest.TestCase):
|
||||||
for n in range(20):
|
for n in range(20):
|
||||||
self.assertEquals(content.count("Thread%03d\n" % n), 1)
|
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):
|
class CTextIOWrapperTest(TextIOWrapperTest):
|
||||||
|
|
||||||
def test_initialization(self):
|
def test_initialization(self):
|
||||||
|
|
|
@ -345,6 +345,10 @@ C-API
|
||||||
Library
|
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
|
- Issue #4687: Fix accuracy of garbage collection runtimes displayed with
|
||||||
gc.DEBUG_STATS.
|
gc.DEBUG_STATS.
|
||||||
|
|
||||||
|
|
|
@ -440,11 +440,7 @@ buffered_close(buffered *self, PyObject *args)
|
||||||
res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
|
res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
|
||||||
ENTER_BUFFERED(self)
|
ENTER_BUFFERED(self)
|
||||||
if (res == NULL) {
|
if (res == NULL) {
|
||||||
/* If flush() fails, just give up */
|
goto end;
|
||||||
if (PyErr_ExceptionMatches(PyExc_IOError))
|
|
||||||
PyErr_Clear();
|
|
||||||
else
|
|
||||||
goto end;
|
|
||||||
}
|
}
|
||||||
Py_XDECREF(res);
|
Py_XDECREF(res);
|
||||||
|
|
||||||
|
|
|
@ -169,6 +169,7 @@ PyDoc_STRVAR(flush_doc,
|
||||||
static PyObject *
|
static PyObject *
|
||||||
bytesio_flush(bytesio *self)
|
bytesio_flush(bytesio *self)
|
||||||
{
|
{
|
||||||
|
CHECK_CLOSED(self);
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -183,11 +183,7 @@ iobase_close(PyObject *self, PyObject *args)
|
||||||
res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
|
res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
|
||||||
PyObject_SetAttrString(self, "__IOBase_closed", Py_True);
|
PyObject_SetAttrString(self, "__IOBase_closed", Py_True);
|
||||||
if (res == NULL) {
|
if (res == NULL) {
|
||||||
/* If flush() fails, just give up */
|
return NULL;
|
||||||
if (PyErr_ExceptionMatches(PyExc_IOError))
|
|
||||||
PyErr_Clear();
|
|
||||||
else
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
Py_XDECREF(res);
|
Py_XDECREF(res);
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
|
|
|
@ -2398,16 +2398,30 @@ static PyObject *
|
||||||
textiowrapper_close(textio *self, PyObject *args)
|
textiowrapper_close(textio *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *res;
|
PyObject *res;
|
||||||
|
int r;
|
||||||
CHECK_INITIALIZED(self);
|
CHECK_INITIALIZED(self);
|
||||||
res = PyObject_CallMethod((PyObject *)self, "flush", NULL);
|
|
||||||
if (res == NULL) {
|
|
||||||
/* If flush() fails, just give up */
|
|
||||||
PyErr_Clear();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
Py_DECREF(res);
|
|
||||||
|
|
||||||
return PyObject_CallMethod(self->buffer, "close", NULL);
|
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 {
|
||||||
|
res = PyObject_CallMethod((PyObject *)self, "flush", NULL);
|
||||||
|
if (res == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
Py_DECREF(res);
|
||||||
|
|
||||||
|
return PyObject_CallMethod(self->buffer, "close", NULL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
|
Loading…
Reference in New Issue