Skip to content

Commit c0a9859

Browse files
[3.10] gh-89047: Fix msecs computation so you never end up with 1000 msecs. (GH-96340) (GH-96342)
1 parent a2cf984 commit c0a9859

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
@@ -325,7 +325,7 @@ def __init__(self, name, level, pathname, lineno,
325325
self.lineno = lineno
326326
self.funcName = func
327327
self.created = ct
328-
self.msecs = (ct - int(ct)) * 1000
328+
self.msecs = int((ct - int(ct)) * 1000) + 0.0 # see gh-89047
329329
self.relativeCreated = (self.created - _startTime) * 1000
330330
if logThreads:
331331
self.thread = threading.get_ident()

Lib/test/test_logging.py

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

4023+
def test_issue_89047(self):
4024+
f = logging.Formatter(fmt='{asctime}.{msecs:03.0f} {message}', style='{', datefmt="%Y-%m-%d %H:%M:%S")
4025+
for i in range(2500):
4026+
time.sleep(0.0004)
4027+
r = logging.makeLogRecord({'msg': 'Message %d' % (i + 1)})
4028+
s = f.format(r)
4029+
self.assertNotIn('.1000', s)
4030+
40234031

40244032
class TestBufferingFormatter(logging.BufferingFormatter):
40254033
def formatHeader(self, records):

0 commit comments

Comments
 (0)