Skip to content

Commit b76a4a5

Browse files
[3.13] gh-116263: Do not rollover empty files in RotatingFileHandler (GH-122788) (#122814)
gh-116263: Do not rollover empty files in RotatingFileHandler (GH-122788) (cherry picked from commit 6094c6f) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 3906181 commit b76a4a5

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

Lib/logging/handlers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,12 @@ def shouldRollover(self, record):
196196
if self.stream is None: # delay was set...
197197
self.stream = self._open()
198198
if self.maxBytes > 0: # are we rolling over?
199+
pos = self.stream.tell()
200+
if not pos:
201+
# gh-116263: Never rollover an empty file
202+
return False
199203
msg = "%s\n" % self.format(record)
200-
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
201-
if self.stream.tell() + len(msg) >= self.maxBytes:
204+
if pos + len(msg) >= self.maxBytes:
202205
# See bpo-45401: Never rollover anything other than regular files
203206
if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
204207
return False

Lib/test/test_logging.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6195,13 +6195,28 @@ def test_emit_after_closing_in_write_mode(self):
61956195
self.assertEqual(fp.read().strip(), '1')
61966196

61976197
class RotatingFileHandlerTest(BaseFileTest):
6198-
@unittest.skipIf(support.is_wasi, "WASI does not have /dev/null.")
61996198
def test_should_not_rollover(self):
6200-
# If maxbytes is zero rollover never occurs
6199+
# If file is empty rollover never occurs
6200+
rh = logging.handlers.RotatingFileHandler(
6201+
self.fn, encoding="utf-8", maxBytes=1)
6202+
self.assertFalse(rh.shouldRollover(None))
6203+
rh.close()
6204+
6205+
# If maxBytes is zero rollover never occurs
62016206
rh = logging.handlers.RotatingFileHandler(
62026207
self.fn, encoding="utf-8", maxBytes=0)
62036208
self.assertFalse(rh.shouldRollover(None))
62046209
rh.close()
6210+
6211+
with open(self.fn, 'wb') as f:
6212+
f.write(b'\n')
6213+
rh = logging.handlers.RotatingFileHandler(
6214+
self.fn, encoding="utf-8", maxBytes=0)
6215+
self.assertFalse(rh.shouldRollover(None))
6216+
rh.close()
6217+
6218+
@unittest.skipIf(support.is_wasi, "WASI does not have /dev/null.")
6219+
def test_should_not_rollover_non_file(self):
62056220
# bpo-45401 - test with special file
62066221
# We set maxBytes to 1 so that rollover would normally happen, except
62076222
# for the check for regular files
@@ -6211,18 +6226,47 @@ def test_should_not_rollover(self):
62116226
rh.close()
62126227

62136228
def test_should_rollover(self):
6214-
rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8", maxBytes=1)
6229+
with open(self.fn, 'wb') as f:
6230+
f.write(b'\n')
6231+
rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8", maxBytes=2)
62156232
self.assertTrue(rh.shouldRollover(self.next_rec()))
62166233
rh.close()
62176234

62186235
def test_file_created(self):
62196236
# checks that the file is created and assumes it was created
62206237
# by us
6238+
os.unlink(self.fn)
62216239
rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8")
62226240
rh.emit(self.next_rec())
62236241
self.assertLogFile(self.fn)
62246242
rh.close()
62256243

6244+
def test_max_bytes(self, delay=False):
6245+
kwargs = {'delay': delay} if delay else {}
6246+
os.unlink(self.fn)
6247+
rh = logging.handlers.RotatingFileHandler(
6248+
self.fn, encoding="utf-8", backupCount=2, maxBytes=100, **kwargs)
6249+
self.assertIs(os.path.exists(self.fn), not delay)
6250+
small = logging.makeLogRecord({'msg': 'a'})
6251+
large = logging.makeLogRecord({'msg': 'b'*100})
6252+
self.assertFalse(rh.shouldRollover(small))
6253+
self.assertFalse(rh.shouldRollover(large))
6254+
rh.emit(small)
6255+
self.assertLogFile(self.fn)
6256+
self.assertFalse(os.path.exists(self.fn + ".1"))
6257+
self.assertFalse(rh.shouldRollover(small))
6258+
self.assertTrue(rh.shouldRollover(large))
6259+
rh.emit(large)
6260+
self.assertTrue(os.path.exists(self.fn))
6261+
self.assertLogFile(self.fn + ".1")
6262+
self.assertFalse(os.path.exists(self.fn + ".2"))
6263+
self.assertTrue(rh.shouldRollover(small))
6264+
self.assertTrue(rh.shouldRollover(large))
6265+
rh.close()
6266+
6267+
def test_max_bytes_delay(self):
6268+
self.test_max_bytes(delay=True)
6269+
62266270
def test_rollover_filenames(self):
62276271
def namer(name):
62286272
return name + ".test"
@@ -6231,11 +6275,15 @@ def namer(name):
62316275
rh.namer = namer
62326276
rh.emit(self.next_rec())
62336277
self.assertLogFile(self.fn)
6278+
self.assertFalse(os.path.exists(namer(self.fn + ".1")))
62346279
rh.emit(self.next_rec())
62356280
self.assertLogFile(namer(self.fn + ".1"))
6281+
self.assertFalse(os.path.exists(namer(self.fn + ".2")))
62366282
rh.emit(self.next_rec())
62376283
self.assertLogFile(namer(self.fn + ".2"))
62386284
self.assertFalse(os.path.exists(namer(self.fn + ".3")))
6285+
rh.emit(self.next_rec())
6286+
self.assertFalse(os.path.exists(namer(self.fn + ".3")))
62396287
rh.close()
62406288

62416289
def test_namer_rotator_inheritance(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:class:`logging.handlers.RotatingFileHandler` no longer rolls over empty log
2+
files.

0 commit comments

Comments
 (0)