Skip to content

Commit

Permalink
bpo-45401: Change shouldRollover() methods to only rollover regular f… (
Browse files Browse the repository at this point in the history
GH-28822) (#28867)

…iles.

Also changed some historical return values from 1 -> True and 0 -> False.
(cherry picked from commit 62a6677)

Co-authored-by: Vinay Sajip <vinay_sajip@yahoo.co.uk>

Co-authored-by: Vinay Sajip <vinay_sajip@yahoo.co.uk>
  • Loading branch information
miss-islington and vsajip committed Oct 11, 2021
1 parent 3a58d60 commit 5aca34f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
14 changes: 10 additions & 4 deletions Lib/logging/handlers.py
Expand Up @@ -187,14 +187,17 @@ def shouldRollover(self, record):
Basically, see if the supplied record would cause the file to exceed
the size limit we have.
"""
# See bpo-45401: Never rollover anything other than regular files
if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
return False
if self.stream is None: # delay was set...
self.stream = self._open()
if self.maxBytes > 0: # are we rolling over?
msg = "%s\n" % self.format(record)
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
if self.stream.tell() + len(msg) >= self.maxBytes:
return 1
return 0
return True
return False

class TimedRotatingFileHandler(BaseRotatingHandler):
"""
Expand Down Expand Up @@ -345,10 +348,13 @@ def shouldRollover(self, record):
record is not used, as we are just comparing times, but it is needed so
the method signatures are the same
"""
# See bpo-45401: Never rollover anything other than regular files
if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
return False
t = int(time.time())
if t >= self.rolloverAt:
return 1
return 0
return True
return False

def getFilesToDelete(self):
"""
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_logging.py
Expand Up @@ -5190,6 +5190,13 @@ def test_should_not_rollover(self):
self.fn, encoding="utf-8", maxBytes=0)
self.assertFalse(rh.shouldRollover(None))
rh.close()
# bpo-45401 - test with special file
# We set maxBytes to 1 so that rollover would normally happen, except
# for the check for regular files
rh = logging.handlers.RotatingFileHandler(
os.devnull, encoding="utf-8", maxBytes=1)
self.assertFalse(rh.shouldRollover(self.next_rec()))
rh.close()

def test_should_rollover(self):
rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8", maxBytes=1)
Expand Down Expand Up @@ -5284,6 +5291,14 @@ def rotator(source, dest):
rh.close()

class TimedRotatingFileHandlerTest(BaseFileTest):
def test_should_not_rollover(self):
# See bpo-45401. Should only ever rollover regular files
fh = logging.handlers.TimedRotatingFileHandler(
os.devnull, 'S', encoding="utf-8", backupCount=1)
time.sleep(1.1) # a little over a second ...
r = logging.makeLogRecord({'msg': 'testing - device file'})
self.assertFalse(fh.shouldRollover(r))

# other test methods added below
def test_rollover(self):
fh = logging.handlers.TimedRotatingFileHandler(
Expand Down

0 comments on commit 5aca34f

Please sign in to comment.