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

Patch by Serhiy Storchaka.
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index fa77ec1..9cbb364 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -346,8 +346,10 @@
         This method has no effect if the file is already closed.
         """
         if not self.__closed:
-            self.flush()
-            self.__closed = True
+            try:
+                self.flush()
+            finally:
+                self.__closed = True
 
     def __del__(self):
         """Destructor.  Calls close()."""
@@ -1584,8 +1586,10 @@
 
     def close(self):
         if self.buffer is not None and not self.closed:
-            self.flush()
-            self.buffer.close()
+            try:
+                self.flush()
+            finally:
+                self.buffer.close()
 
     @property
     def closed(self):