]> granicus.if.org Git - python/commitdiff
bpo-34068: _io__IOBase_close_impl could call _PyObject_SetAttrId with an exception...
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 17 Jul 2018 07:33:14 +0000 (10:33 +0300)
committerGitHub <noreply@github.com>
Tue, 17 Jul 2018 07:33:14 +0000 (10:33 +0300)
(cherry picked from commit 28f07364f066792ceee93231dbb80ae8ad98b2bb)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Lib/test/test_io.py
Misc/NEWS.d/next/Core and Builtins/2018-07-14-08-58-46.bpo-34068.9xfM55.rst [new file with mode: 0644]
Modules/_io/iobase.c

index 88fd6ce4a6beae039c5f7b96f64c830926579e31..f306917ca7da2f5e2bb38b867d81b33094a14c07 100644 (file)
@@ -957,6 +957,16 @@ class IOTest(unittest.TestCase):
                 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):
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-07-14-08-58-46.bpo-34068.9xfM55.rst b/Misc/NEWS.d/next/Core and Builtins/2018-07-14-08-58-46.bpo-34068.9xfM55.rst
new file mode 100644 (file)
index 0000000..0ed8ff9
--- /dev/null
@@ -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.
index c8642040ae426a8e41ca7cd6652cbe954304d226..f5c82c14096c3f482887568859707e49bd43b30c 100644 (file)
@@ -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)