diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-05-03 16:25:33 +0000 |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-05-03 16:25:33 +0000 |
commit | f7fd8e4c9492defbb32cc142faaf3abeabd00cf8 (patch) | |
tree | 8854e7a740532f8b6b746e9e5c4118b95ad758f9 /Modules/_io/textio.c | |
parent | c3ce9e357dac697561a8c97e977e49db321571e3 (diff) | |
download | cpython-git-f7fd8e4c9492defbb32cc142faaf3abeabd00cf8.tar.gz |
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 'Modules/_io/textio.c')
-rw-r--r-- | Modules/_io/textio.c | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 90a4c09f7a..c5d7b85a47 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -2405,16 +2405,30 @@ static PyObject * 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 * |