cpython: b0602a1eb3f6 (original) (raw)
Mercurial > cpython
changeset 80956:b0602a1eb3f6 3.3
call close on the underlying stream even if flush raises (closes #16597) Patch by Serhiy Storchaka. [#16597]
Benjamin Peterson benjamin@python.org | |
---|---|
date | Thu, 20 Dec 2012 11:53:11 -0600 |
parents | c744b6f8a09a |
children | 142012e47c3b 54372f38932e |
files | Lib/_pyio.py Lib/test/test_io.py Misc/NEWS Modules/_io/bufferedio.c Modules/_io/textio.c |
diffstat | 5 files changed, 80 insertions(+), 13 deletions(-)[+] [-] Lib/_pyio.py 12 Lib/test/test_io.py 28 Misc/NEWS 3 Modules/_io/bufferedio.c 26 Modules/_io/textio.c 24 |
line wrap: on
line diff
--- a/Lib/_pyio.py +++ b/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()[](#l1.7)
self.__closed = True[](#l1.8)
try:[](#l1.9)
self.flush()[](#l1.10)
finally:[](#l1.11)
self.__closed = True[](#l1.12)
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()[](#l1.20)
self.buffer.close()[](#l1.21)
try:[](#l1.22)
self.flush()[](#l1.23)
finally:[](#l1.24)
self.buffer.close()[](#l1.25)
--- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -603,6 +603,7 @@ class IOTest(unittest.TestCase): raise IOError() f.flush = bad_flush self.assertRaises(IOError, f.close) # exception not swallowed
self.assertTrue(f.closed)[](#l2.7)
def test_multi_close(self): f = self.open(support.TESTFN, "wb", buffering=0) @@ -780,6 +781,22 @@ class CommonBufferedTests: raw.flush = bad_flush b = self.tp(raw) self.assertRaises(IOError, b.close) # exception not swallowed
self.assertTrue(b.closed)[](#l2.15)
- def test_close_error_on_close(self):
raw = self.MockRawIO()[](#l2.18)
def bad_flush():[](#l2.19)
raise IOError('flush')[](#l2.20)
def bad_close():[](#l2.21)
raise IOError('close')[](#l2.22)
raw.close = bad_close[](#l2.23)
b = self.tp(raw)[](#l2.24)
b.flush = bad_flush[](#l2.25)
with self.assertRaises(IOError) as err: # exception not swallowed[](#l2.26)
b.close()[](#l2.27)
self.assertEqual(err.exception.args, ('close',))[](#l2.28)
self.assertEqual(err.exception.__context__.args, ('flush',))[](#l2.29)
self.assertFalse(b.closed)[](#l2.30)
def test_multi_close(self): raw = self.MockRawIO() @@ -1296,6 +1313,16 @@ class BufferedWriterTest(unittest.TestCa with self.assertRaises(TypeError): self.tp(self.MockRawIO(), 8, 12)
- def test_write_error_on_close(self):
raw = self.MockRawIO()[](#l2.39)
def bad_write(b):[](#l2.40)
raise IOError()[](#l2.41)
raw.write = bad_write[](#l2.42)
b = self.tp(raw)[](#l2.43)
b.write(b'spam')[](#l2.44)
self.assertRaises(IOError, b.close) # exception not swallowed[](#l2.45)
self.assertTrue(b.closed)[](#l2.46)
+ class CBufferedWriterTest(BufferedWriterTest, SizeofTest): tp = io.BufferedWriter @@ -2465,6 +2492,7 @@ class TextIOWrapperTest(unittest.TestCas raise IOError() txt.flush = bad_flush self.assertRaises(IOError, txt.close) # exception not swallowed
self.assertTrue(txt.closed)[](#l2.55)
def test_multi_close(self): txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,9 @@ What's New in Python 3.3.1? Core and Builtins ----------------- +- Issue #16597: Make BufferedIO.close call close() on the underlying stream if
--- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -484,7 +484,7 @@ buffered_closed_get(buffered *self, void static PyObject * buffered_close(buffered *self, PyObject *args) {
- PyObject *res = NULL, *exc = NULL, *val, *tb; int r; CHECK_INITIALIZED(self) @@ -512,13 +512,29 @@ buffered_close(buffered *self, PyObject res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL); if (!ENTER_BUFFERED(self)) return NULL;
res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_close, NULL);
- if (exc != NULL) {
if (res != NULL) {[](#l4.28)
Py_CLEAR(res);[](#l4.29)
PyErr_Restore(exc, val, tb);[](#l4.30)
}[](#l4.31)
else {[](#l4.32)
PyObject *val2;[](#l4.33)
Py_DECREF(exc);[](#l4.34)
Py_XDECREF(tb);[](#l4.35)
PyErr_Fetch(&exc, &val2, &tb);[](#l4.36)
PyErr_NormalizeException(&exc, &val2, &tb);[](#l4.37)
PyException_SetContext(val2, val);[](#l4.38)
PyErr_Restore(exc, val2, tb);[](#l4.39)
}[](#l4.40)
- }
+ end: LEAVE_BUFFERED(self) return res;
--- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -2554,6 +2554,7 @@ textiowrapper_close(textio self, PyObje Py_RETURN_NONE; / stream already closed */ } else {
PyObject *exc = NULL, *val, *tb;[](#l5.7) if (self->deallocating) {[](#l5.8) res = _PyObject_CallMethodId(self->buffer, &PyId__dealloc_warn, "O", self);[](#l5.9) if (res)[](#l5.10)
@@ -2562,13 +2563,28 @@ textiowrapper_close(textio *self, PyObje PyErr_Clear(); } res = _PyObject_CallMethodId((PyObject *)self, &PyId_flush, NULL);
if (res == NULL) {[](#l5.15)
return NULL;[](#l5.16)
}[](#l5.17)
if (res == NULL)[](#l5.18)
PyErr_Fetch(&exc, &val, &tb);[](#l5.19) else[](#l5.20) Py_DECREF(res);[](#l5.21)
return _PyObject_CallMethodId(self->buffer, &PyId_close, NULL);[](#l5.23)
res = _PyObject_CallMethodId(self->buffer, &PyId_close, NULL);[](#l5.24)
if (exc != NULL) {[](#l5.25)
if (res != NULL) {[](#l5.26)
Py_CLEAR(res);[](#l5.27)
PyErr_Restore(exc, val, tb);[](#l5.28)
}[](#l5.29)
else {[](#l5.30)
PyObject *val2;[](#l5.31)
Py_DECREF(exc);[](#l5.32)
Py_XDECREF(tb);[](#l5.33)
PyErr_Fetch(&exc, &val2, &tb);[](#l5.34)
PyErr_NormalizeException(&exc, &val2, &tb);[](#l5.35)
PyException_SetContext(val2, val);[](#l5.36)
PyErr_Restore(exc, val2, tb);[](#l5.37)
}[](#l5.38)
}[](#l5.39)
} }return res;[](#l5.40)