gh-116263: Do not rollover empty files in RotatingFileHandler (GH-122788)

This commit is contained in:
Serhiy Storchaka 2024-08-08 09:48:11 +03:00 committed by GitHub
parent 3f76b6b8ac
commit 6094c6fc2f
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...
self.stream = self._open()
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)
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
if self.stream.tell() + len(msg) >= self.maxBytes:
if pos + len(msg) >= self.maxBytes:
# 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