Repository navigation
gh-102402: Fix floating point math issue by using time.time_ns() in logging.LogRecord
#102412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
2f046c2
2460f88
059f856
997bd41
2e7cde3
5505cea
411a421
d3a1f78
5fa370b
39cab9d
920bf74
8bdc406
bf68b26
e404ca0
9661499
ec74b0e
710f3ac
ff8140c
fd45694
84b931b
5a604f9
dc90919
502ff65
65ea1b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4310,6 +4310,23 @@ def test_issue_89047(self): | |
| s = f.format(r) | ||
| self.assertNotIn('.1000', s) | ||
|
|
||
| def test_issue_102402_100msecs(self): | ||
|
serhiy-storchaka marked this conversation as resolved.
Outdated
|
||
| og_time_ns = time.time_ns | ||
| tests = ( | ||
| # (time_ns, expected_msecs_value) | ||
| (1_677_902_297_100_000_000, 100.0), # exactly 100ms | ||
| (1_677_903_920_999_998_503, 999.0), # check truncating doesn't round | ||
| (1_677_903_920_000_998_503, 0.0), # check truncating doesn't round | ||
|
|
||
|
serhiy-storchaka marked this conversation as resolved.
Outdated
|
||
| ) | ||
| try: | ||
| for ns, want in tests: | ||
| time.time_ns = lambda: ns | ||
| record = logging.makeLogRecord({'msg': 'test'}) | ||
| self.assertEqual(record.msecs, want) | ||
| finally: | ||
| time.time_ns = og_time_ns | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: Is there a more preferred way to accomplish this other than monkeypatching
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use import time
from unittest.mock import patch
for t in (123, 456, 789):
with patch('time.time_ns') as patched_ns:
patched_ns.return_value = t
value = time.time_ns()
print(f'patched: {value}')
print(f'OG: {time.time_ns()}')which should, when run, print something like this:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, my assumption was that we probably didn't want to add imports (I don't know why I assumed that...). Updated to patch with |
||
|
|
||
|
|
||
| class TestBufferingFormatter(logging.BufferingFormatter): | ||
| def formatHeader(self, records): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Adjust ``logging.LogRecord`` to use ``time.time_ns()`` and fix minor bug | ||
| related to floating point math. |
Uh oh!
There was an error while loading. Please reload this page.