Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit16d1085

Browse files
committed
gh-62948: IOBase finalizer logs close() exceptions
1 parent4b65d56 commit16d1085

File tree

4 files changed

+15
-24
lines changed

4 files changed

+15
-24
lines changed

‎Doc/whatsnew/3.13.rst

Lines changed: 9 additions & 0 deletions
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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ def byteslike(*pos, **kw):
6666
classEmptyStruct(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_DEBUGorsys.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
withself.assertRaises(AttributeError):
12191215
self.tp(rawio).xyzzy
12201216

1221-
ifnotIOBASE_EMITS_UNRAISABLE:
1222-
self.assertIsNone(cm.unraisable)
1223-
elifcm.unraisableisnotNone:
1224-
self.assertEqual(cm.unraisable.exc_type,OSError)
1217+
self.assertEqual(cm.unraisable.exc_type,OSError)
12251218

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

3025-
ifnotIOBASE_EMITS_UNRAISABLE:
3026-
self.assertIsNone(cm.unraisable)
3027-
elifcm.unraisableisnotNone:
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 0 additions & 12 deletions
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-
#ifndefPy_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)

[8]ページ先頭

©2009-2025 Movatter.jp