[Python-checkins] bpo-38716: stop rotating handlers from setting inherited namer and rotator to None (GH-17072)

Vinay Sajip webhook-mailer at python.org
Wed Nov 6 16:22:03 EST 2019


https://github.com/python/cpython/commit/519cb8772a9745b1c7d8218cabcd2f96ceda4d62
commit: 519cb8772a9745b1c7d8218cabcd2f96ceda4d62
branch: master
author: l0rb <lorbritzer at yahoo.de>
committer: Vinay Sajip <vinay_sajip at yahoo.co.uk>
date: 2019-11-06T21:21:40Z
summary:

bpo-38716: stop rotating handlers from setting inherited namer and rotator to None (GH-17072)

files:
A Misc/NEWS.d/next/Library/2019-11-06-15-58-07.bpo-38716.R3uMLT.rst
M Lib/logging/handlers.py
M Lib/test/test_logging.py

diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
index 5641fee573556..c1aec9880d72a 100644
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -48,6 +48,9 @@ class BaseRotatingHandler(logging.FileHandler):
     Not meant to be instantiated directly.  Instead, use RotatingFileHandler
     or TimedRotatingFileHandler.
     """
+    namer = None
+    rotator = None
+
     def __init__(self, filename, mode, encoding=None, delay=False, errors=None):
         """
         Use the specified filename for streamed logging
@@ -58,8 +61,6 @@ def __init__(self, filename, mode, encoding=None, delay=False, errors=None):
         self.mode = mode
         self.encoding = encoding
         self.errors = errors
-        self.namer = None
-        self.rotator = None
 
     def emit(self, record):
         """
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 53b5bfc93f33e..6de8803081e51 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -5030,6 +5030,25 @@ def namer(name):
         self.assertFalse(os.path.exists(namer(self.fn + ".3")))
         rh.close()
 
+    def test_namer_rotator_inheritance(self):
+        class HandlerWithNamerAndRotator(logging.handlers.RotatingFileHandler):
+            def namer(self, name):
+                return name + ".test"
+
+            def rotator(self, source, dest):
+                if os.path.exists(source):
+                    os.rename(source, dest + ".rotated")
+
+        rh = HandlerWithNamerAndRotator(
+            self.fn, backupCount=2, maxBytes=1)
+        self.assertEqual(rh.namer(self.fn), self.fn + ".test")
+        rh.emit(self.next_rec())
+        self.assertLogFile(self.fn)
+        rh.emit(self.next_rec())
+        self.assertLogFile(rh.namer(self.fn + ".1") + ".rotated")
+        self.assertFalse(os.path.exists(rh.namer(self.fn + ".1")))
+        rh.close()
+
     @support.requires_zlib
     def test_rotator(self):
         def namer(name):
diff --git a/Misc/NEWS.d/next/Library/2019-11-06-15-58-07.bpo-38716.R3uMLT.rst b/Misc/NEWS.d/next/Library/2019-11-06-15-58-07.bpo-38716.R3uMLT.rst
new file mode 100644
index 0000000000000..906eb6b6cd60a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-11-06-15-58-07.bpo-38716.R3uMLT.rst
@@ -0,0 +1 @@
+logging: change RotatingHandler namer and rotator to class-level attributes. This stops __init__ from setting them to None in the case where a subclass defines them with eponymous methods.



More information about the Python-checkins mailing list