Skip to content

Commit 5d51cc9

Browse files
committed
pythongh-62948: IOBase finalizer logs close() exceptions
1 parent adccff3 commit 5d51cc9

File tree

4 files changed

+15
-24
lines changed

4 files changed

+15
-24
lines changed

Doc/whatsnew/3.13.rst

+9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ New Modules
8787
Improved Modules
8888
================
8989

90+
io
91+
--
92+
93+
The :class:`io.IOBase` finalizer now logs the ``close()`` method errors with
94+
:data:`sys.excepthook`. Previously, errors were ignored silently by default,
95+
and only logged in :ref:`Python Development Mode <devmode>` or on :ref:`Python
96+
built on debug mode <debug-build>`.
97+
(Contributed by Victor Stinner in :gh:`62948`.)
98+
9099
pathlib
91100
-------
92101

Lib/test/test_io.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ def byteslike(*pos, **kw):
6666
class EmptyStruct(ctypes.Structure):
6767
pass
6868

69-
# Does io.IOBase finalizer log the exception if the close() method fails?
70-
# The exception is ignored silently by default in release build.
71-
IOBASE_EMITS_UNRAISABLE = (support.Py_DEBUG or sys.flags.dev_mode)
72-
7369

7470
def _default_chunk_size():
7571
"""Get the default TextIOWrapper chunk size"""
@@ -1218,10 +1214,7 @@ def test_error_through_destructor(self):
12181214
with self.assertRaises(AttributeError):
12191215
self.tp(rawio).xyzzy
12201216

1221-
if not IOBASE_EMITS_UNRAISABLE:
1222-
self.assertIsNone(cm.unraisable)
1223-
elif cm.unraisable is not None:
1224-
self.assertEqual(cm.unraisable.exc_type, OSError)
1217+
self.assertEqual(cm.unraisable.exc_type, OSError)
12251218

12261219
def test_repr(self):
12271220
raw = self.MockRawIO()
@@ -3022,10 +3015,7 @@ def test_error_through_destructor(self):
30223015
with self.assertRaises(AttributeError):
30233016
self.TextIOWrapper(rawio, encoding="utf-8").xyzzy
30243017

3025-
if not IOBASE_EMITS_UNRAISABLE:
3026-
self.assertIsNone(cm.unraisable)
3027-
elif cm.unraisable is not None:
3028-
self.assertEqual(cm.unraisable.exc_type, OSError)
3018+
self.assertEqual(cm.unraisable.exc_type, OSError)
30293019

30303020
# Systematic tests of the text I/O API
30313021

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The :class:`io.IOBase` finalizer now logs the ``close()`` method errors with
2+
:data:`sys.excepthook`. Previously, errors were ignored silently by default,
3+
and only logged in :ref:`Python Development Mode <devmode>` or on
4+
:ref:`Python built on debug mode <debug-build>`. Patch by Victor Stinner.

Modules/_io/iobase.c

-12
Original file line numberDiff line numberDiff line change
@@ -319,20 +319,8 @@ iobase_finalize(PyObject *self)
319319
if (PyObject_SetAttr(self, &_Py_ID(_finalizing), Py_True))
320320
PyErr_Clear();
321321
res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(close));
322-
/* Silencing I/O errors is bad, but printing spurious tracebacks is
323-
equally as bad, and potentially more frequent (because of
324-
shutdown issues). */
325322
if (res == NULL) {
326-
#ifndef Py_DEBUG
327-
if (_Py_GetConfig()->dev_mode) {
328-
PyErr_WriteUnraisable(self);
329-
}
330-
else {
331-
PyErr_Clear();
332-
}
333-
#else
334323
PyErr_WriteUnraisable(self);
335-
#endif
336324
}
337325
else {
338326
Py_DECREF(res);

0 commit comments

Comments
 (0)