From 2529340acd564397c1fbcf679fa4748d3edd02be Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 17 Apr 2024 14:50:38 +0200 Subject: [PATCH 1/2] gh-102402: Fix logging test_relativeCreated_has_higher_precision() leak Fix a reference leak in test_relativeCreated_has_higher_precision() of test_logging: don't reimport the logging the logging module. --- Lib/test/test_logging.py | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 3f0b363066df2c..7d7c3d55af2fab 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -4573,23 +4573,19 @@ def test_relativeCreated_has_higher_precision(self): # See issue gh-102402 ns = 1_677_903_920_000_998_503 # approx. 2023-03-04 04:25:20 UTC offsets_ns = (200, 500, 12_354, 99_999, 1_677_903_456_999_123_456) - orig_modules = import_helper._save_and_remove_modules(['logging']) - try: - with patch("time.time_ns") as patched_ns: - # mock for module import - patched_ns.return_value = ns - import logging - for offset_ns in offsets_ns: - new_ns = ns + offset_ns - # mock for log record creation - patched_ns.return_value = new_ns - record = logging.makeLogRecord({'msg': 'test'}) - self.assertAlmostEqual(record.created, new_ns / 1e9, places=6) - # After PR gh-102412, precision (places) increases from 3 to 7 - self.assertAlmostEqual(record.relativeCreated, offset_ns / 1e6, places=7) - finally: - import_helper._save_and_remove_modules(['logging']) - sys.modules.update(orig_modules) + + with (patch("time.time_ns") as time_ns_mock, + support.swap_attr(logging, '_startTime', ns)): + for offset_ns in offsets_ns: + # mock for log record creation + new_ns = ns + offset_ns + time_ns_mock.return_value = new_ns + + record = logging.makeLogRecord({'msg': 'test'}) + self.assertAlmostEqual(record.created, new_ns / 1e9, places=6) + + # After PR gh-102412, precision (places) increases from 3 to 7 + self.assertAlmostEqual(record.relativeCreated, offset_ns / 1e6, places=7) class TestBufferingFormatter(logging.BufferingFormatter): From 83930411aef25990ae4087e7a3236962ef19def0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 17 Apr 2024 22:41:39 +0200 Subject: [PATCH 2/2] Add @cpython_only decorator --- Lib/test/test_logging.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 7d7c3d55af2fab..871c751dc21289 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -4569,6 +4569,8 @@ def test_msecs_has_no_floating_point_precision_loss(self): self.assertEqual(record.msecs, want) self.assertEqual(record.created, ns / 1e9) + # The test overrides a private attribute + @support.cpython_only def test_relativeCreated_has_higher_precision(self): # See issue gh-102402 ns = 1_677_903_920_000_998_503 # approx. 2023-03-04 04:25:20 UTC