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:

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:

@property def closed(self):

--- 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

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

+

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)

+ 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

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) {

res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_close, NULL);

+ 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 {

@@ -2562,13 +2563,28 @@ textiowrapper_close(textio *self, PyObje PyErr_Clear(); } res = _PyObject_CallMethodId((PyObject *)self, &PyId_flush, NULL);