Skip to content

Commit 62a6677

Browse files
authored
bpo-45401: Change shouldRollover() methods to only rollover regular f… (GH-28822)
…iles. Also changed some historical return values from 1 -> True and 0 -> False.
1 parent 0bcc5ad commit 62a6677

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

Lib/logging/handlers.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,17 @@ def shouldRollover(self, record):
187187
Basically, see if the supplied record would cause the file to exceed
188188
the size limit we have.
189189
"""
190+
# See bpo-45401: Never rollover anything other than regular files
191+
if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
192+
return False
190193
if self.stream is None: # delay was set...
191194
self.stream = self._open()
192195
if self.maxBytes > 0: # are we rolling over?
193196
msg = "%s\n" % self.format(record)
194197
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
195198
if self.stream.tell() + len(msg) >= self.maxBytes:
196-
return 1
197-
return 0
199+
return True
200+
return False
198201

199202
class TimedRotatingFileHandler(BaseRotatingHandler):
200203
"""
@@ -345,10 +348,13 @@ def shouldRollover(self, record):
345348
record is not used, as we are just comparing times, but it is needed so
346349
the method signatures are the same
347350
"""
351+
# See bpo-45401: Never rollover anything other than regular files
352+
if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
353+
return False
348354
t = int(time.time())
349355
if t >= self.rolloverAt:
350-
return 1
351-
return 0
356+
return True
357+
return False
352358

353359
def getFilesToDelete(self):
354360
"""

Lib/test/test_logging.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5216,6 +5216,13 @@ def test_should_not_rollover(self):
52165216
self.fn, encoding="utf-8", maxBytes=0)
52175217
self.assertFalse(rh.shouldRollover(None))
52185218
rh.close()
5219+
# bpo-45401 - test with special file
5220+
# We set maxBytes to 1 so that rollover would normally happen, except
5221+
# for the check for regular files
5222+
rh = logging.handlers.RotatingFileHandler(
5223+
os.devnull, encoding="utf-8", maxBytes=1)
5224+
self.assertFalse(rh.shouldRollover(self.next_rec()))
5225+
rh.close()
52195226

52205227
def test_should_rollover(self):
52215228
rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8", maxBytes=1)
@@ -5310,6 +5317,14 @@ def rotator(source, dest):
53105317
rh.close()
53115318

53125319
class TimedRotatingFileHandlerTest(BaseFileTest):
5320+
def test_should_not_rollover(self):
5321+
# See bpo-45401. Should only ever rollover regular files
5322+
fh = logging.handlers.TimedRotatingFileHandler(
5323+
os.devnull, 'S', encoding="utf-8", backupCount=1)
5324+
time.sleep(1.1) # a little over a second ...
5325+
r = logging.makeLogRecord({'msg': 'testing - device file'})
5326+
self.assertFalse(fh.shouldRollover(r))
5327+
53135328
# other test methods added below
53145329
def test_rollover(self):
53155330
fh = logging.handlers.TimedRotatingFileHandler(

0 commit comments

Comments
 (0)