Skip to content

Commit

Permalink
bpo-38716: stop rotating handlers from setting inherited namer and ro…
Browse files Browse the repository at this point in the history
…tator to None (GH-17072)
  • Loading branch information
l0rb authored and vsajip committed Nov 6, 2019
1 parent 5c0c325 commit 519cb87
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Lib/logging/handlers.py
Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_logging.py
Expand Up @@ -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):
Expand Down
@@ -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.

0 comments on commit 519cb87

Please sign in to comment.