Skip to content

Commit 7cb8d28

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

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
@@ -190,9 +190,12 @@ def shouldRollover(self, record):
190190
if self.stream is None: # delay was set...
191191
self.stream = self._open()
192192
if self.maxBytes > 0: # are we rolling over?
193+
pos = self.stream.tell()
194+
if not pos:
195+
# gh-116263: Never rollover an empty file
196+
return False
193197
msg = "%s\n" % self.format(record)
194-
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
195-
if self.stream.tell() + len(msg) >= self.maxBytes:
198+
if pos + len(msg) >= self.maxBytes:
196199
# See bpo-45401: Never rollover anything other than regular files
197200
if os.path.exists(self.baseFilename) and not os.path.isfile(self.baseFilename):
198201
return False

Lib/test/test_logging.py

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

60676067
class RotatingFileHandlerTest(BaseFileTest):
6068-
@unittest.skipIf(support.is_wasi, "WASI does not have /dev/null.")
60696068
def test_should_not_rollover(self):
6070-
# If maxbytes is zero rollover never occurs
6069+
# If file is empty rollover never occurs
6070+
rh = logging.handlers.RotatingFileHandler(
6071+
self.fn, encoding="utf-8", maxBytes=1)
6072+
self.assertFalse(rh.shouldRollover(None))
6073+
rh.close()
6074+
6075+
# If maxBytes is zero rollover never occurs
60716076
rh = logging.handlers.RotatingFileHandler(
60726077
self.fn, encoding="utf-8", maxBytes=0)
60736078
self.assertFalse(rh.shouldRollover(None))
60746079
rh.close()
6080+
6081+
with open(self.fn, 'wb') as f:
6082+
f.write(b'\n')
6083+
rh = logging.handlers.RotatingFileHandler(
6084+
self.fn, encoding="utf-8", maxBytes=0)
6085+
self.assertFalse(rh.shouldRollover(None))
6086+
rh.close()
6087+
6088+
@unittest.skipIf(support.is_wasi, "WASI does not have /dev/null.")
6089+
def test_should_not_rollover_non_file(self):
60756090
# bpo-45401 - test with special file
60766091
# We set maxBytes to 1 so that rollover would normally happen, except
60776092
# for the check for regular files
@@ -6081,18 +6096,47 @@ def test_should_not_rollover(self):
60816096
rh.close()
60826097

60836098
def test_should_rollover(self):
6084-
rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8", maxBytes=1)
6099+
with open(self.fn, 'wb') as f:
6100+
f.write(b'\n')
6101+
rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8", maxBytes=2)
60856102
self.assertTrue(rh.shouldRollover(self.next_rec()))
60866103
rh.close()
60876104

60886105
def test_file_created(self):
60896106
# checks that the file is created and assumes it was created
60906107
# by us
6108+
os.unlink(self.fn)
60916109
rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8")
60926110
rh.emit(self.next_rec())
60936111
self.assertLogFile(self.fn)
60946112
rh.close()
60956113

6114+
def test_max_bytes(self, delay=False):
6115+
kwargs = {'delay': delay} if delay else {}
6116+
os.unlink(self.fn)
6117+
rh = logging.handlers.RotatingFileHandler(
6118+
self.fn, encoding="utf-8", backupCount=2, maxBytes=100, **kwargs)
6119+
self.assertIs(os.path.exists(self.fn), not delay)
6120+
small = logging.makeLogRecord({'msg': 'a'})
6121+
large = logging.makeLogRecord({'msg': 'b'*100})
6122+
self.assertFalse(rh.shouldRollover(small))
6123+
self.assertFalse(rh.shouldRollover(large))
6124+
rh.emit(small)
6125+
self.assertLogFile(self.fn)
6126+
self.assertFalse(os.path.exists(self.fn + ".1"))
6127+
self.assertFalse(rh.shouldRollover(small))
6128+
self.assertTrue(rh.shouldRollover(large))
6129+
rh.emit(large)
6130+
self.assertTrue(os.path.exists(self.fn))
6131+
self.assertLogFile(self.fn + ".1")
6132+
self.assertFalse(os.path.exists(self.fn + ".2"))
6133+
self.assertTrue(rh.shouldRollover(small))
6134+
self.assertTrue(rh.shouldRollover(large))
6135+
rh.close()
6136+
6137+
def test_max_bytes_delay(self):
6138+
self.test_max_bytes(delay=True)
6139+
60966140
def test_rollover_filenames(self):
60976141
def namer(name):
60986142
return name + ".test"
@@ -6101,11 +6145,15 @@ def namer(name):
61016145
rh.namer = namer
61026146
rh.emit(self.next_rec())
61036147
self.assertLogFile(self.fn)
6148+
self.assertFalse(os.path.exists(namer(self.fn + ".1")))
61046149
rh.emit(self.next_rec())
61056150
self.assertLogFile(namer(self.fn + ".1"))
6151+
self.assertFalse(os.path.exists(namer(self.fn + ".2")))
61066152
rh.emit(self.next_rec())
61076153
self.assertLogFile(namer(self.fn + ".2"))
61086154
self.assertFalse(os.path.exists(namer(self.fn + ".3")))
6155+
rh.emit(self.next_rec())
6156+
self.assertFalse(os.path.exists(namer(self.fn + ".3")))
61096157
rh.close()
61106158

61116159
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)