summaryrefslogtreecommitdiffstats
path: root/Lib/_pyio.py
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-05-31 11:41:19 (GMT)
committerGitHub <noreply@github.com>2023-05-31 11:41:19 (GMT)
commit58a2e0981642dcddf49daa776ff68a43d3498cee (patch)
tree3dd438b025dc884fecf9f850f19eda79d4fa9f8f /Lib/_pyio.py
parent85e5d03163cac106ac8ec142ef03f1349a48948b (diff)
downloadcpython-58a2e0981642dcddf49daa776ff68a43d3498cee.zip
cpython-58a2e0981642dcddf49daa776ff68a43d3498cee.tar.gz
cpython-58a2e0981642dcddf49daa776ff68a43d3498cee.tar.bz2
gh-62948: IOBase finalizer logs close() errors (#105104)
Diffstat (limited to 'Lib/_pyio.py')
-rw-r--r--Lib/_pyio.py20
1 files changed, 4 insertions, 16 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 7f247ff..32698ab 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -33,11 +33,8 @@ DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
# Rebind for compatibility
BlockingIOError = BlockingIOError
-# Does io.IOBase finalizer log the exception if the close() method fails?
-# The exception is ignored silently by default in release build.
-_IOBASE_EMITS_UNRAISABLE = (hasattr(sys, "gettotalrefcount") or sys.flags.dev_mode)
# Does open() check its 'errors' argument?
-_CHECK_ERRORS = _IOBASE_EMITS_UNRAISABLE
+_CHECK_ERRORS = (hasattr(sys, "gettotalrefcount") or sys.flags.dev_mode)
def text_encoding(encoding, stacklevel=2):
@@ -416,18 +413,9 @@ class IOBase(metaclass=abc.ABCMeta):
if closed:
return
- if _IOBASE_EMITS_UNRAISABLE:
- self.close()
- else:
- # The try/except block is in case this is called at program
- # exit time, when it's possible that globals have already been
- # deleted, and then the close() call might fail. Since
- # there's nothing we can do about such failures and they annoy
- # the end users, we suppress the traceback.
- try:
- self.close()
- except:
- pass
+ # If close() fails, the caller logs the exception with
+ # sys.unraisablehook. close() must be called at the end at __del__().
+ self.close()
### Inquiries ###