Skip to content

Commit b5e50fa

Browse files
committed
gh-135683: Ensure that WatchedFileHandler calls handleError on exceptions.
1 parent 43fdb70 commit b5e50fa

3 files changed

Lines changed: 44 additions & 9 deletions

File tree

Lib/logging/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2022 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2026 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -18,7 +18,7 @@
1818
Logging package for Python. Based on PEP 282 and comments thereto in
1919
comp.lang.python.
2020
21-
Copyright (C) 2001-2022 Vinay Sajip. All Rights Reserved.
21+
Copyright (C) 2001-2026 Vinay Sajip. All Rights Reserved.
2222
2323
To use, simply 'import logging' and log away!
2424
"""
@@ -1257,7 +1257,10 @@ def emit(self, record):
12571257
"""
12581258
if self.stream is None:
12591259
if self.mode != 'w' or not self._closed:
1260-
self.stream = self._open()
1260+
try:
1261+
self.stream = self._open()
1262+
except Exception:
1263+
self.handleError(record)
12611264
if self.stream:
12621265
StreamHandler.emit(self, record)
12631266

Lib/logging/handlers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2021 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2026 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -18,7 +18,7 @@
1818
Additional handlers for the logging package for Python. The core package is
1919
based on PEP 282 and comments thereto in comp.lang.python.
2020
21-
Copyright (C) 2001-2021 Vinay Sajip. All Rights Reserved.
21+
Copyright (C) 2001-2026 Vinay Sajip. All Rights Reserved.
2222
2323
To use, simply 'import logging.handlers' and log away!
2424
"""
@@ -540,7 +540,10 @@ def emit(self, record):
540540
If underlying file has changed, reopen the file before emitting the
541541
record to it.
542542
"""
543-
self.reopenIfNeeded()
543+
try:
544+
self.reopenIfNeeded()
545+
except Exception:
546+
self.handleError(record)
544547
logging.FileHandler.emit(self, record)
545548

546549

Lib/test/test_logging.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2022 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2026 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -16,7 +16,7 @@
1616

1717
"""Test harness for the logging module. Run all tests.
1818
19-
Copyright (C) 2001-2022 Vinay Sajip. All Rights Reserved.
19+
Copyright (C) 2001-2026 Vinay Sajip. All Rights Reserved.
2020
"""
2121
import logging
2222
import logging.handlers
@@ -800,6 +800,36 @@ def lock_holder_thread_fn():
800800

801801
support.wait_process(pid, exitcode=0)
802802

803+
def test_135683(self):
804+
# See gh-135683
805+
806+
def tester():
807+
with tempfile.TemporaryDirectory(prefix='test_logging_') as tmp:
808+
tmp_dir = pathlib.Path(tmp)
809+
dir_path = tmp_dir / 'dir'
810+
link_path = tmp_dir / 'link'
811+
dir_path.mkdir()
812+
link_path.symlink_to(dir_path)
813+
r = logging.makeLogRecord({'msg': 'Test'})
814+
h = logging.handlers.WatchedFileHandler(link_path / 'file.log')
815+
m = Mock()
816+
h.handleError = m
817+
try:
818+
h.handle(r)
819+
m.assert_not_called()
820+
shutil.rmtree(dir_path)
821+
h.handle(r)
822+
m.assert_called()
823+
finally:
824+
h.close()
825+
826+
old_raiseExceptions = logging.raiseExceptions
827+
try:
828+
for test_value in (False, True):
829+
logging.raiseExceptions = test_value
830+
tester()
831+
finally:
832+
logging.raiseExceptions = old_raiseExceptions
803833

804834
class BadStream(object):
805835
def write(self, data):
@@ -4439,7 +4469,6 @@ def test_queue_listener_with_multiple_handlers(self):
44394469

44404470
if hasattr(logging.handlers, 'QueueListener'):
44414471
import multiprocessing
4442-
from unittest.mock import patch
44434472

44444473
@skip_if_tsan_fork
44454474
@threading_helper.requires_working_threading()

0 commit comments

Comments
 (0)