Skip to content

Commit 8f25cc9

Browse files
authored
gh-102402: Fix logging test_relativeCreated_has_higher_precision() leak (#117985)
Fix a reference leak in test_relativeCreated_has_higher_precision() of test_logging: don't reimport the logging the logging module.
1 parent ccdcd1d commit 8f25cc9

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

Lib/test/test_logging.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4598,27 +4598,25 @@ def test_msecs_has_no_floating_point_precision_loss(self):
45984598
self.assertEqual(record.msecs, want)
45994599
self.assertEqual(record.created, ns / 1e9)
46004600

4601+
# The test overrides a private attribute
4602+
@support.cpython_only
46014603
def test_relativeCreated_has_higher_precision(self):
46024604
# See issue gh-102402
46034605
ns = 1_677_903_920_000_998_503 # approx. 2023-03-04 04:25:20 UTC
46044606
offsets_ns = (200, 500, 12_354, 99_999, 1_677_903_456_999_123_456)
4605-
orig_modules = import_helper._save_and_remove_modules(['logging'])
4606-
try:
4607-
with patch("time.time_ns") as patched_ns:
4608-
# mock for module import
4609-
patched_ns.return_value = ns
4610-
import logging
4611-
for offset_ns in offsets_ns:
4612-
new_ns = ns + offset_ns
4613-
# mock for log record creation
4614-
patched_ns.return_value = new_ns
4615-
record = logging.makeLogRecord({'msg': 'test'})
4616-
self.assertAlmostEqual(record.created, new_ns / 1e9, places=6)
4617-
# After PR gh-102412, precision (places) increases from 3 to 7
4618-
self.assertAlmostEqual(record.relativeCreated, offset_ns / 1e6, places=7)
4619-
finally:
4620-
import_helper._save_and_remove_modules(['logging'])
4621-
sys.modules.update(orig_modules)
4607+
4608+
with (patch("time.time_ns") as time_ns_mock,
4609+
support.swap_attr(logging, '_startTime', ns)):
4610+
for offset_ns in offsets_ns:
4611+
# mock for log record creation
4612+
new_ns = ns + offset_ns
4613+
time_ns_mock.return_value = new_ns
4614+
4615+
record = logging.makeLogRecord({'msg': 'test'})
4616+
self.assertAlmostEqual(record.created, new_ns / 1e9, places=6)
4617+
4618+
# After PR gh-102412, precision (places) increases from 3 to 7
4619+
self.assertAlmostEqual(record.relativeCreated, offset_ns / 1e6, places=7)
46224620

46234621

46244622
class TestBufferingFormatter(logging.BufferingFormatter):

0 commit comments

Comments
 (0)