Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-34068: _io__IOBase_close_impl could call _PyObject_SetAttrId with an exception set #8282

Merged
merged 2 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,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,2 @@
In :meth:`io.IOBase.close`, ensure that ``_PyObject_SetAttrId()`` is not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the changelog, you should try to avoid to mention low-level C internals. You can say something like: "In io.IOBase.close, ensure that the closed attribute is not set to True with a live exception. (...)".

called with a live exception. Patch by Zackery Spytz and Serhiy Storchaka.
12 changes: 7 additions & 5 deletions Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ static PyObject *
_io__IOBase_close_impl(PyObject *self)
/*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/
{
PyObject *res;
int closed = iobase_is_closed(self);
PyObject *res, *exc, *val, *tb;
int rc, closed = iobase_is_closed(self);

if (closed < 0) {
return NULL;
Expand All @@ -236,9 +236,11 @@ _io__IOBase_close_impl(PyObject *self)

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