Skip to content

Commit

Permalink
[3.6] bpo-34068: _io__IOBase_close_impl could call _PyObject_SetAttrI…
Browse files Browse the repository at this point in the history
…d with an exception set (pythonGH-8282).

(cherry picked from commit 28f0736)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
  • Loading branch information
ZackerySpytz authored and serhiy-storchaka committed Jul 17, 2018
1 parent 8b5d191 commit f780462
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,16 @@ def read1(self, size):
self.assertSequenceEqual(buffer[result:], unused)
self.assertEqual(len(reader.avail), avail - result)

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.
11 changes: 7 additions & 4 deletions Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,19 @@ static PyObject *
_io__IOBase_close_impl(PyObject *self)
/*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/
{
PyObject *res;
PyObject *res, *exc, *val, *tb;
int rc;

if (IS_CLOSED(self))
Py_RETURN_NONE;

res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);

if (_PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True) < 0) {
Py_XDECREF(res);
return NULL;
PyErr_Fetch(&exc, &val, &tb);
rc = _PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True);
_PyErr_ChainExceptions(exc, val, tb);
if (rc < 0) {
Py_CLEAR(res);
}

if (res == NULL)
Expand Down

0 comments on commit f780462

Please sign in to comment.