Skip to content

Commit 4f6f7c5

Browse files
authored
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.
1 parent 8a8b59c commit 4f6f7c5

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

‎Lib/_pyio.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,16 @@ def close(self):
405405

406406
def __del__(self):
407407
"""Destructor. Calls close()."""
408+
try:
409+
closed = self.closed
410+
except Exception:
411+
# If getting closed fails, then the object is probably
412+
# in an unusable state, so ignore.
413+
return
414+
415+
if closed:
416+
return
417+
408418
if _IOBASE_EMITS_UNRAISABLE:
409419
self.close()
410420
else:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:class:`_pyio.IOBase` destructor now does nothing if getting the ``closed``
2+
attribute fails to better mimick :class:`_io.IOBase` finalizer.

0 commit comments

Comments
 (0)