diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2016-07-22 18:23:04 +0100 |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2016-07-22 18:23:04 +0100 |
commit | bc72d8635aadf9302dd3bd72acdb6d0039ec5cc4 (patch) | |
tree | e4d063242459fb0ffcc2ef07f42fdea2fc0001ed /Lib | |
parent | ba7c21ae5a444cebdf8478b632a4ab796d484785 (diff) | |
download | cpython-bc72d8635aadf9302dd3bd72acdb6d0039ec5cc4.tar.gz |
Closes #27493: accepted Path objects in file handlers for logging.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/logging/__init__.py | 6 | ||||
-rw-r--r-- | Lib/logging/handlers.py | 3 | ||||
-rw-r--r-- | Lib/test/test_logging.py | 24 |
3 files changed, 31 insertions, 2 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index f941f4884b..fd422ea1e5 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1,4 +1,4 @@ -# Copyright 2001-2015 by Vinay Sajip. All Rights Reserved. +# Copyright 2001-2016 by Vinay Sajip. All Rights Reserved. # # Permission to use, copy, modify, and distribute this software and its # documentation for any purpose and without fee is hereby granted, @@ -18,7 +18,7 @@ Logging package for Python. Based on PEP 282 and comments thereto in comp.lang.python. -Copyright (C) 2001-2015 Vinay Sajip. All Rights Reserved. +Copyright (C) 2001-2016 Vinay Sajip. All Rights Reserved. To use, simply 'import logging' and log away! """ @@ -994,6 +994,8 @@ class FileHandler(StreamHandler): """ Open the specified file and use it as the stream for logging. """ + # Issue #27493: add support for Path objects to be passed in + filename = os.fspath(filename) #keep the absolute path, otherwise derived classes which use this #may come a cropper when the current directory changes self.baseFilename = os.path.abspath(filename) diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index 296d6cfa30..ba00a69139 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -246,6 +246,9 @@ class TimedRotatingFileHandler(BaseRotatingHandler): self.extMatch = re.compile(self.extMatch, re.ASCII) self.interval = self.interval * interval # multiply by units requested + # The following line added because the filename passed in could be a + # path object (see Issue #27493), but self.baseFilename will be a string + filename = self.baseFilename if os.path.exists(filename): t = os.stat(filename)[ST_MTIME] else: diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 9e9a439eab..e998f6038e 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -26,6 +26,7 @@ import logging.config import codecs import configparser import datetime +import pathlib import pickle import io import gc @@ -575,6 +576,29 @@ class HandlerTest(BaseTest): self.assertFalse(h.shouldFlush(r)) h.close() + def test_path_objects(self): + """ + Test that Path objects are accepted as filename arguments to handlers. + + See Issue #27493. + """ + fd, fn = tempfile.mkstemp() + os.close(fd) + os.unlink(fn) + pfn = pathlib.Path(fn) + cases = ( + (logging.FileHandler, (pfn, 'w')), + (logging.handlers.RotatingFileHandler, (pfn, 'a')), + (logging.handlers.TimedRotatingFileHandler, (pfn, 'h')), + ) + if sys.platform in ('linux', 'darwin'): + cases += ((logging.handlers.WatchedFileHandler, (pfn, 'w')),) + for cls, args in cases: + h = cls(*args) + self.assertTrue(os.path.exists(fn)) + os.unlink(fn) + h.close() + @unittest.skipIf(os.name == 'nt', 'WatchedFileHandler not appropriate for Windows.') @unittest.skipUnless(threading, 'Threading required for this test.') def test_race(self): |