Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
638e6220
Commit
638e6220
authored
Jul 22, 2016
by
Vinay Sajip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Closes #27493: accepted Path objects in file handlers for logging.
parent
d3afb62b
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
44 additions
and
2 deletions
+44
-2
Doc/library/logging.handlers.rst
Doc/library/logging.handlers.rst
+13
-0
Lib/logging/__init__.py
Lib/logging/__init__.py
+4
-2
Lib/logging/handlers.py
Lib/logging/handlers.py
+3
-0
Lib/test/test_logging.py
Lib/test/test_logging.py
+24
-0
No files found.
Doc/library/logging.handlers.rst
View file @
638e6220
...
@@ -84,6 +84,9 @@ sends logging output to a disk file. It inherits the output functionality from
...
@@ -84,6 +84,9 @@ sends logging output to a disk file. It inherits the output functionality from
with that encoding. If *delay* is true, then file opening is deferred until the
with that encoding. If *delay* is true, then file opening is deferred until the
first call to :meth:`emit`. By default, the file grows indefinitely.
first call to :meth:`emit`. By default, the file grows indefinitely.
.. versionchanged:: 3.6
As well as string values, :class:`~pathlib.Path` objects are also accepted
for the *filename* argument.
.. method:: close()
.. method:: close()
...
@@ -160,6 +163,9 @@ for this value.
...
@@ -160,6 +163,9 @@ for this value.
with that encoding. If *delay* is true, then file opening is deferred until the
with that encoding. If *delay* is true, then file opening is deferred until the
first call to :meth:`emit`. By default, the file grows indefinitely.
first call to :meth:`emit`. By default, the file grows indefinitely.
.. versionchanged:: 3.6
As well as string values, :class:`~pathlib.Path` objects are also accepted
for the *filename* argument.
.. method:: reopenIfNeeded()
.. method:: reopenIfNeeded()
...
@@ -287,6 +293,9 @@ module, supports rotation of disk log files.
...
@@ -287,6 +293,9 @@ module, supports rotation of disk log files.
:file:`app.log.2`, etc. exist, then they are renamed to :file:`app.log.2`,
:file:`app.log.2`, etc. exist, then they are renamed to :file:`app.log.2`,
:file:`app.log.3` etc. respectively.
:file:`app.log.3` etc. respectively.
.. versionchanged:: 3.6
As well as string values, :class:`~pathlib.Path` objects are also accepted
for the *filename* argument.
.. method:: doRollover()
.. method:: doRollover()
...
@@ -365,6 +374,10 @@ timed intervals.
...
@@ -365,6 +374,10 @@ timed intervals.
.. versionchanged:: 3.4
.. versionchanged:: 3.4
*atTime* parameter was added.
*atTime* parameter was added.
.. versionchanged:: 3.6
As well as string values, :class:`~pathlib.Path` objects are also accepted
for the *filename* argument.
.. method:: doRollover()
.. method:: doRollover()
Does a rollover, as described above.
Does a rollover, as described above.
...
...
Lib/logging/__init__.py
View file @
638e6220
# Copyright 2001-201
5
by Vinay Sajip. All Rights Reserved.
# Copyright 2001-201
6
by Vinay Sajip. All Rights Reserved.
#
#
# Permission to use, copy, modify, and distribute this software and its
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# documentation for any purpose and without fee is hereby granted,
...
@@ -18,7 +18,7 @@
...
@@ -18,7 +18,7 @@
Logging package for Python. Based on PEP 282 and comments thereto in
Logging package for Python. Based on PEP 282 and comments thereto in
comp.lang.python.
comp.lang.python.
Copyright (C) 2001-201
5
Vinay Sajip. All Rights Reserved.
Copyright (C) 2001-201
6
Vinay Sajip. All Rights Reserved.
To use, simply 'import logging' and log away!
To use, simply 'import logging' and log away!
"""
"""
...
@@ -994,6 +994,8 @@ class FileHandler(StreamHandler):
...
@@ -994,6 +994,8 @@ class FileHandler(StreamHandler):
"""
"""
Open the specified file and use it as the stream for logging.
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
#keep the absolute path, otherwise derived classes which use this
#may come a cropper when the current directory changes
#may come a cropper when the current directory changes
self
.
baseFilename
=
os
.
path
.
abspath
(
filename
)
self
.
baseFilename
=
os
.
path
.
abspath
(
filename
)
...
...
Lib/logging/handlers.py
View file @
638e6220
...
@@ -246,6 +246,9 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
...
@@ -246,6 +246,9 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
self
.
extMatch
=
re
.
compile
(
self
.
extMatch
,
re
.
ASCII
)
self
.
extMatch
=
re
.
compile
(
self
.
extMatch
,
re
.
ASCII
)
self
.
interval
=
self
.
interval
*
interval
# multiply by units requested
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
):
if
os
.
path
.
exists
(
filename
):
t
=
os
.
stat
(
filename
)[
ST_MTIME
]
t
=
os
.
stat
(
filename
)[
ST_MTIME
]
else
:
else
:
...
...
Lib/test/test_logging.py
View file @
638e6220
...
@@ -26,6 +26,7 @@ import logging.config
...
@@ -26,6 +26,7 @@ import logging.config
import
codecs
import
codecs
import
configparser
import
configparser
import
datetime
import
datetime
import
pathlib
import
pickle
import
pickle
import
io
import
io
import
gc
import
gc
...
@@ -575,6 +576,29 @@ class HandlerTest(BaseTest):
...
@@ -575,6 +576,29 @@ class HandlerTest(BaseTest):
self.assertFalse(h.shouldFlush(r))
self.assertFalse(h.shouldFlush(r))
h.close()
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.skipIf(os.name == 'nt', 'WatchedFileHandler not appropriate for Windows.')
@unittest.skipUnless(threading, 'Threading required for this test.')
@unittest.skipUnless(threading, 'Threading required for this test.')
def test_race(self):
def test_race(self):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment