Skip to content

Commit 659910e

Browse files
Fix test_emit_open_error on Windows
Split the WatchedFileHandler case into a separate test skipped on Windows, where open log files are locked and the directory holding them cannot be removed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent af834ad commit 659910e

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

Lib/test/test_logging.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6416,37 +6416,45 @@ def test_emit_after_closing_in_write_mode(self):
64166416
with open(self.fn) as fp:
64176417
self.assertEqual(fp.read().strip(), '1')
64186418

6419-
def test_emit_open_error(self):
6419+
def _check_open_error(self, h):
64206420
# gh-135683: an error while opening the file in emit() respects
64216421
# raiseExceptions, like an error during the actual write.
6422-
d = tempfile.mkdtemp()
6423-
self.addCleanup(os_helper.rmtree, d)
64246422
r = logging.makeLogRecord({})
64256423
old_raise = logging.raiseExceptions
64266424
self.addCleanup(setattr, logging, 'raiseExceptions', old_raise)
64276425

6426+
logging.raiseExceptions = True
6427+
with support.captured_stderr() as stderr:
6428+
h.handle(r)
6429+
self.assertIn('\nFileNotFoundError:', stderr.getvalue())
6430+
6431+
logging.raiseExceptions = False
6432+
with support.captured_stderr() as stderr:
6433+
h.handle(r)
6434+
self.assertEqual('', stderr.getvalue())
6435+
6436+
def test_emit_open_error(self):
64286437
# FileHandler with delay: the failing open happens in emit().
6429-
fh = logging.FileHandler(os.path.join(d, 'missing', 'a.log'),
6430-
encoding='utf-8', delay=True)
6431-
self.addCleanup(fh.close)
6438+
d = tempfile.mkdtemp()
6439+
self.addCleanup(os_helper.rmtree, d)
6440+
h = logging.FileHandler(os.path.join(d, 'missing', 'a.log'),
6441+
encoding='utf-8', delay=True)
6442+
self.addCleanup(h.close)
6443+
self._check_open_error(h)
6444+
6445+
@unittest.skipIf(os.name == 'nt',
6446+
'WatchedFileHandler not appropriate for Windows.')
6447+
def test_emit_reopen_error(self):
64326448
# WatchedFileHandler: reopenIfNeeded() fails after the dir is removed.
6449+
d = tempfile.mkdtemp()
6450+
self.addCleanup(os_helper.rmtree, d)
64336451
subdir = os.path.join(d, 'sub')
64346452
os.mkdir(subdir)
6435-
wfh = logging.handlers.WatchedFileHandler(
6453+
h = logging.handlers.WatchedFileHandler(
64366454
os.path.join(subdir, 'b.log'), encoding='utf-8')
6437-
self.addCleanup(wfh.close)
6455+
self.addCleanup(h.close)
64386456
os_helper.rmtree(subdir)
6439-
6440-
for h in (fh, wfh):
6441-
logging.raiseExceptions = True
6442-
with support.captured_stderr() as stderr:
6443-
h.handle(r)
6444-
self.assertIn('\nFileNotFoundError:', stderr.getvalue())
6445-
6446-
logging.raiseExceptions = False
6447-
with support.captured_stderr() as stderr:
6448-
h.handle(r)
6449-
self.assertEqual('', stderr.getvalue())
6457+
self._check_open_error(h)
64506458

64516459
class RotatingFileHandlerTest(BaseFileTest):
64526460
def test_should_not_rollover(self):

0 commit comments

Comments
 (0)