From a856a3006db9400d8b2671f34263677a95b5c9e1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 11 Jun 2019 01:52:48 +0200 Subject: [PATCH 1/2] bpo-18748: Fix _pyio.IOBase destructor (closed case) _pyio.IOBase destructor now does nothing if getting the closed attribute fails to better mimick _io.IOBase finalizer. --- Lib/_pyio.py | 10 ++++++++++ .../Library/2019-06-11-01-54-19.bpo-18748.ADqCkq.rst | 2 ++ 2 files changed, 12 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-06-11-01-54-19.bpo-18748.ADqCkq.rst diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 43c24342ad6162..0b6493bc8dc926 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 + if _IOBASE_EMITS_UNRAISABLE: self.close() else: 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 00000000000000..08836c0166ea8c --- /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. From 707b3f55aeca32f68237448d924d29a49f40cb51 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 11 Jun 2019 02:19:09 +0200 Subject: [PATCH 2/2] Fix typo in NEWS entry --- .../next/Library/2019-06-11-01-54-19.bpo-18748.ADqCkq.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 08836c0166ea8c..295ddebb2a4015 100644 --- 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 @@ -1,2 +1,2 @@ :class:`_pyio.IOBase` destructor now does nothing if getting the ``closed`` -attribute fails to better mimick :class`_io.IOBase` finalizer. +attribute fails to better mimick :class:`_io.IOBase` finalizer.