[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 6094c6fc2f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-09-02 14:07:27 +02:00 committed by GitHub
parent 39061814f2
commit b76a4a5db7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 58 additions and 5 deletions

View File

@ -196,9 +196,12 @@ class RotatingFileHandler(BaseRotatingHandler):
if self.stream is None: # delay was set... if self.stream is None: # delay was set...
self.stream = self._open() self.stream = self._open()
if self.maxBytes > 0: # are we rolling over? if self.maxBytes > 0: # are we rolling over?
pos = self.stream.tell()
if not pos:
# gh-116263: Never rollover an empty file
return False
msg = "%s\n" % self.format(record) msg = "%s\n" % self.format(record)
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature if pos + len(msg) >= self.maxBytes:
if self.stream.tell() + len(msg) >= self.maxBytes:
# See bpo-45401: Never rollover anything other than regular files # See bpo-45401: Never rollover anything other than regular files
if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename): if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
return False return False

View File

@ -6195,13 +6195,28 @@ class FileHandlerTest(BaseFileTest):
self.assertEqual(fp.read().strip(), '1') self.assertEqual(fp.read().strip(), '1')
class RotatingFileHandlerTest(BaseFileTest): class RotatingFileHandlerTest(BaseFileTest):
@unittest.skipIf(support.is_wasi, "WASI does not have /dev/null.")
def test_should_not_rollover(self): def test_should_not_rollover(self):
# If maxbytes is zero rollover never occurs # If file is empty rollover never occurs
rh = logging.handlers.RotatingFileHandler(
self.fn, encoding="utf-8", maxBytes=1)
self.assertFalse(rh.shouldRollover(None))
rh.close()
# If maxBytes is zero rollover never occurs
rh = logging.handlers.RotatingFileHandler( rh = logging.handlers.RotatingFileHandler(
self.fn, encoding="utf-8", maxBytes=0) self.fn, encoding="utf-8", maxBytes=0)
self.assertFalse(rh.shouldRollover(None)) self.assertFalse(rh.shouldRollover(None))
rh.close() rh.close()
with open(self.fn, 'wb') as f:
f.write(b'\n')
rh = logging.handlers.RotatingFileHandler(
self.fn, encoding="utf-8", maxBytes=0)
self.assertFalse(rh.shouldRollover(None))
rh.close()
@unittest.skipIf(support.is_wasi, "WASI does not have /dev/null.")
def test_should_not_rollover_non_file(self):
# bpo-45401 - test with special file # bpo-45401 - test with special file
# We set maxBytes to 1 so that rollover would normally happen, except # We set maxBytes to 1 so that rollover would normally happen, except
# for the check for regular files # for the check for regular files
@ -6211,18 +6226,47 @@ class RotatingFileHandlerTest(BaseFileTest):
rh.close() rh.close()
def test_should_rollover(self): def test_should_rollover(self):
rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8", maxBytes=1) with open(self.fn, 'wb') as f:
f.write(b'\n')
rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8", maxBytes=2)
self.assertTrue(rh.shouldRollover(self.next_rec())) self.assertTrue(rh.shouldRollover(self.next_rec()))
rh.close() rh.close()
def test_file_created(self): def test_file_created(self):
# checks that the file is created and assumes it was created # checks that the file is created and assumes it was created
# by us # by us
os.unlink(self.fn)
rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8") rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8")
rh.emit(self.next_rec()) rh.emit(self.next_rec())
self.assertLogFile(self.fn) self.assertLogFile(self.fn)
rh.close() rh.close()
def test_max_bytes(self, delay=False):
kwargs = {'delay': delay} if delay else {}
os.unlink(self.fn)
rh = logging.handlers.RotatingFileHandler(
self.fn, encoding="utf-8", backupCount=2, maxBytes=100, **kwargs)
self.assertIs(os.path.exists(self.fn), not delay)
small = logging.makeLogRecord({'msg': 'a'})
large = logging.makeLogRecord({'msg': 'b'*100})
self.assertFalse(rh.shouldRollover(small))
self.assertFalse(rh.shouldRollover(large))
rh.emit(small)
self.assertLogFile(self.fn)
self.assertFalse(os.path.exists(self.fn + ".1"))
self.assertFalse(rh.shouldRollover(small))
self.assertTrue(rh.shouldRollover(large))
rh.emit(large)
self.assertTrue(os.path.exists(self.fn))
self.assertLogFile(self.fn + ".1")
self.assertFalse(os.path.exists(self.fn + ".2"))
self.assertTrue(rh.shouldRollover(small))
self.assertTrue(rh.shouldRollover(large))
rh.close()
def test_max_bytes_delay(self):
self.test_max_bytes(delay=True)
def test_rollover_filenames(self): def test_rollover_filenames(self):
def namer(name): def namer(name):
return name + ".test" return name + ".test"
@ -6231,11 +6275,15 @@ class RotatingFileHandlerTest(BaseFileTest):
rh.namer = namer rh.namer = namer
rh.emit(self.next_rec()) rh.emit(self.next_rec())
self.assertLogFile(self.fn) self.assertLogFile(self.fn)
self.assertFalse(os.path.exists(namer(self.fn + ".1")))
rh.emit(self.next_rec()) rh.emit(self.next_rec())
self.assertLogFile(namer(self.fn + ".1")) self.assertLogFile(namer(self.fn + ".1"))
self.assertFalse(os.path.exists(namer(self.fn + ".2")))
rh.emit(self.next_rec()) rh.emit(self.next_rec())
self.assertLogFile(namer(self.fn + ".2")) self.assertLogFile(namer(self.fn + ".2"))
self.assertFalse(os.path.exists(namer(self.fn + ".3"))) self.assertFalse(os.path.exists(namer(self.fn + ".3")))
rh.emit(self.next_rec())
self.assertFalse(os.path.exists(namer(self.fn + ".3")))
rh.close() rh.close()
def test_namer_rotator_inheritance(self): def test_namer_rotator_inheritance(self):

View File

@ -0,0 +1,2 @@
:class:`logging.handlers.RotatingFileHandler` no longer rolls over empty log
files.