[Python-checkins] bpo-18748: Fix _pyio.IOBase destructor (closed case) (GH-13952)

Victor Stinner webhook-mailer at python.org
Mon Jun 10 20:49:17 EDT 2019

commit: 4f6f7c5a611905fb6b81671547f268c226bc646a
branch: master
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2019-06-11T02:49:06+02:00

bpo-18748: Fix _pyio.IOBase destructor (closed case) (GH-13952)

_pyio.IOBase destructor now does nothing if getting the closed
attribute fails to better mimick _io.IOBase finalizer.

A Misc/NEWS.d/next/Library/2019-06-11-01-54-19.bpo-18748.ADqCkq.rst
M Lib/_pyio.py

diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 43c24342ad61..0b6493bc8dc9 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -405,6 +405,16 @@ def close(self):
     def __del__(self):
         """Destructor.  Calls close()."""
+        try:
+            closed = self.closed
+        except Exception:
+            # If getting closed fails, then the object is probably
+            # in an unusable state, so ignore.
+            return
+        if closed:
+            return
diff --git a/Misc/NEWS.d/next/Library/2019-06-11-01-54-19.bpo-18748.ADqCkq.rst b/Misc/NEWS.d/next/Library/2019-06-11-01-54-19.bpo-18748.ADqCkq.rst
new file mode 100644
index 000000000000..295ddebb2a40
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-06-11-01-54-19.bpo-18748.ADqCkq.rst
@@ -0,0 +1,2 @@
+:class:`_pyio.IOBase` destructor now does nothing if getting the ``closed``
+attribute fails to better mimick :class:`_io.IOBase` finalizer.

More information about the Python-checkins mailing list