Commit fc153d12 authored by Serhiy Storchaka's avatar Serhiy Storchaka Committed by GitHub

[2.7] bpo-34068: iobase_close could call PyObject_SetAttrString with an...

[2.7] bpo-34068: iobase_close could call PyObject_SetAttrString with an exception set (GH-8282). (GH-8312) (GH-8314)

(cherry picked from commit 28f07364)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>.
(cherry picked from commit cc13016658a9ed86d0b702ab6c251ad5952a952f)
parent a45fa39d
......@@ -690,6 +690,16 @@ class IOTest(unittest.TestCase):
self.assertEqual(stream.readinto(buffer), 5)
self.assertEqual(buffer.tobytes(), b"12345")
def test_close_assert(self):
class R(self.IOBase):
def __setattr__(self, name, value):
pass
def flush(self):
raise OSError()
f = R()
# This would cause an assertion failure.
self.assertRaises(OSError, f.close)
class CIOTest(IOTest):
......
In :meth:`io.IOBase.close`, ensure that the :attr:`~io.IOBase.closed`
attribute is not set with a live exception. Patch by Zackery Spytz and Serhiy
Storchaka.
......@@ -177,17 +177,25 @@ _PyIOBase_check_closed(PyObject *self, PyObject *args)
static PyObject *
iobase_close(PyObject *self, PyObject *args)
{
PyObject *res;
PyObject *res, *exc, *val, *tb;
int rc;
if (IS_CLOSED(self))
Py_RETURN_NONE;
res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
PyObject_SetAttrString(self, "__IOBase_closed", Py_True);
PyErr_Fetch(&exc, &val, &tb);
rc = PyObject_SetAttrString(self, "__IOBase_closed", Py_True);
_PyErr_ReplaceException(exc, val, tb);
if (rc < 0) {
Py_CLEAR(res);
}
if (res == NULL) {
return NULL;
}
Py_XDECREF(res);
Py_DECREF(res);
Py_RETURN_NONE;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment