call close on the underlying stream even if flush raises (#16597)

This commit is contained in:
Benjamin Peterson 2012-12-20 12:24:10 -06:00
parent bacf1bf355
commit a2d6d7121e
4 changed files with 68 additions and 13 deletions

View file

@ -455,7 +455,7 @@ buffered_closed_get(buffered *self, void *context)
static PyObject *
buffered_close(buffered *self, PyObject *args)
{
PyObject *res = NULL;
PyObject *res = NULL, *exc = NULL, *val, *tb;
int r;
CHECK_INITIALIZED(self)
@ -475,13 +475,25 @@ buffered_close(buffered *self, PyObject *args)
res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
if (!ENTER_BUFFERED(self))
return NULL;
if (res == NULL) {
goto end;
}
Py_XDECREF(res);
if (res == NULL)
PyErr_Fetch(&exc, &val, &tb);
else
Py_DECREF(res);
res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_close, NULL);
if (exc != NULL) {
if (res != NULL) {
Py_CLEAR(res);
PyErr_Restore(exc, val, tb);
}
else {
Py_DECREF(exc);
Py_XDECREF(val);
Py_XDECREF(tb);
}
}
end:
LEAVE_BUFFERED(self)
return res;