]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-89047: Fix msecs computation so you never end up with 1000 msecs. (GH-96340)
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Sat, 27 Aug 2022 12:33:24 +0000 (13:33 +0100)
committerGitHub <noreply@github.com>
Sat, 27 Aug 2022 12:33:24 +0000 (13:33 +0100)
Lib/logging/__init__.py
Lib/test/test_logging.py

index afb5234a077295ce579f61924ff7a72513ca561d..c3208a21f499abedd75dd433fc3498c0ac4ab6ee 100644 (file)
@@ -340,7 +340,7 @@ class LogRecord(object):
         self.lineno = lineno
         self.funcName = func
         self.created = ct
-        self.msecs = (ct - int(ct)) * 1000
+        self.msecs = int((ct - int(ct)) * 1000) + 0.0  # see gh-89047
         self.relativeCreated = (self.created - _startTime) * 1000
         if logThreads:
             self.thread = threading.get_ident()
index 99ea2f687551d7a47b6d4c0aa8e1e7b3f4c983dd..a67ed07f12c87cedba93271e79c1e6c7cb08b527 100644 (file)
@@ -4261,6 +4261,14 @@ class FormatterTest(unittest.TestCase, AssertErrorMessage):
         f.converter = time.gmtime
         self.assertEqual(f.formatTime(r), '21/04/1993 08:03:00')
 
+    def test_issue_89047(self):
+        f = logging.Formatter(fmt='{asctime}.{msecs:03.0f} {message}', style='{', datefmt="%Y-%m-%d %H:%M:%S")
+        for i in range(2500):
+            time.sleep(0.0004)
+            r = logging.makeLogRecord({'msg': 'Message %d' % (i + 1)})
+            s = f.format(r)
+            self.assertNotIn('.1000', s)
+
 
 class TestBufferingFormatter(logging.BufferingFormatter):
     def formatHeader(self, records):