Skip to content

Commit 103f26f

Browse files
[3.11] pythongh-89047: Fix msecs computation so you never end up with 1000 msecs. (pythonGH-96340) (pythonGH-96341)
1 parent 698df30 commit 103f26f

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Lib/logging/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def __init__(self, name, level, pathname, lineno,
334334
self.lineno = lineno
335335
self.funcName = func
336336
self.created = ct
337-
self.msecs = (ct - int(ct)) * 1000
337+
self.msecs = int((ct - int(ct)) * 1000) + 0.0 # see gh-89047
338338
self.relativeCreated = (self.created - _startTime) * 1000
339339
if logThreads:
340340
self.thread = threading.get_ident()

Lib/test/test_logging.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4088,6 +4088,14 @@ class NoMsecFormatter(logging.Formatter):
40884088
f.converter = time.gmtime
40894089
self.assertEqual(f.formatTime(r), '21/04/1993 08:03:00')
40904090

4091+
def test_issue_89047(self):
4092+
f = logging.Formatter(fmt='{asctime}.{msecs:03.0f} {message}', style='{', datefmt="%Y-%m-%d %H:%M:%S")
4093+
for i in range(2500):
4094+
time.sleep(0.0004)
4095+
r = logging.makeLogRecord({'msg': 'Message %d' % (i + 1)})
4096+
s = f.format(r)
4097+
self.assertNotIn('.1000', s)
4098+
40914099

40924100
class TestBufferingFormatter(logging.BufferingFormatter):
40934101
def formatHeader(self, records):

0 commit comments

Comments
 (0)