From 4f6f7c5a611905fb6b81671547f268c226bc646a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 11 Jun 2019 02:49:06 +0200 Subject: 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. --- Lib/_pyio.py | 10 ++++++++++ .../next/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 43c2434..0b6493b 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -405,6 +405,16 @@ class IOBase(metaclass=abc.ABCMeta): 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 0000000..295ddeb --- /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. -- cgit v0.12