Skip to content

Commit 40e6f6d

Browse files
committed
pythongh-90535: Warn of deprecation of intervals other than 1 with when='MIDNIGHT'.
1 parent c66dbdd commit 40e6f6d

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Lib/logging/handlers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import queue
2929
import threading
3030
import copy
31+
import warnings
3132

3233
#
3334
# Some constants...
@@ -258,6 +259,11 @@ def __init__(self, filename, when='h', interval=1, backupCount=0,
258259
raise ValueError("Invalid rollover interval specified: %s" % self.when)
259260

260261
self.extMatch = re.compile(self.extMatch, re.ASCII)
262+
if self.when == 'MIDNIGHT' and interval != 1:
263+
msg = (f'An interval of {interval} is incompatible with '
264+
"when='MIDNIGHT'. Use when='D' (for days) "
265+
'if you want to use an interval other than 1.')
266+
warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
261267
self.interval = self.interval * interval # multiply by units requested
262268
# The following line added because the filename passed in could be a
263269
# path object (see Issue #27493), but self.baseFilename will be a string

Lib/test/test_logging.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5755,6 +5755,16 @@ def test_compute_rollover_daily_attime(self):
57555755
finally:
57565756
rh.close()
57575757

5758+
def test_midnight_interval(self):
5759+
with warnings.catch_warnings(record=True) as w:
5760+
rh = logging.handlers.TimedRotatingFileHandler(
5761+
self.fn, encoding="utf-8", when='MIDNIGHT', interval=2, backupCount=0,
5762+
utc=True)
5763+
rh.close()
5764+
if w:
5765+
self.assertIs(w[0].category, DeprecationWarning)
5766+
self.assertIn('An interval of 2 is incompatible ', str(w[0].message))
5767+
57585768
#@unittest.skipIf(True, 'Temporarily skipped while failures investigated.')
57595769
def test_compute_rollover_weekly_attime(self):
57605770
currentTime = int(time.time())

0 commit comments

Comments
 (0)