@@ -4598,25 +4598,55 @@ 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
46034601 def test_relativeCreated_has_higher_precision (self ):
4604- # See issue gh-102402
4605- ns = 1_677_903_920_000_998_503 # approx. 2023-03-04 04:25:20 UTC
4602+ # See issue gh-102402.
4603+ # Run the code in the subprocess, because the time module should
4604+ # be patched before the first import of the logging package.
4605+ # Temporary unloading and re-importing the logging package has
4606+ # side effects (including registering the atexit callback and
4607+ # references leak).
4608+ start_ns = 1_677_903_920_000_998_503 # approx. 2023-03-04 04:25:20 UTC
46064609 offsets_ns = (200 , 500 , 12_354 , 99_999 , 1_677_903_456_999_123_456 )
4610+ code = textwrap .dedent (f"""
4611+ start_ns = { start_ns !r}
4612+ offsets_ns = { offsets_ns !r}
4613+ start_monotonic_ns = start_ns - 1
4614+
4615+ import time
4616+ # Only time.time_ns needs to be patched for the current
4617+ # implementation, but patch also other functions to make
4618+ # the test less implementation depending.
4619+ old_time_ns = time.time_ns
4620+ old_time = time.time
4621+ old_monotonic_ns = time.monotonic_ns
4622+ old_monotonic = time.monotonic
4623+ time_ns_result = start_ns
4624+ time.time_ns = lambda: time_ns_result
4625+ time.time = lambda: time.time_ns()/1e9
4626+ time.monotonic_ns = lambda: time_ns_result - start_monotonic_ns
4627+ time.monotonic = lambda: time.monotonic_ns()/1e9
4628+ try:
4629+ import logging
46074630
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-
4631+ for offset_ns in offsets_ns:
4632+ # mock for log record creation
4633+ time_ns_result = start_ns + offset_ns
4634+ record = logging.makeLogRecord({{'msg': 'test'}})
4635+ print(record.created, record.relativeCreated)
4636+ finally:
4637+ time.time_ns = old_time_ns
4638+ time.time = old_time
4639+ time.monotonic_ns = old_monotonic_ns
4640+ time.monotonic = old_monotonic
4641+ """ )
4642+ rc , out , err = assert_python_ok ("-c" , code )
4643+ out = out .decode ()
4644+ for offset_ns , line in zip (offsets_ns , out .splitlines (), strict = True ):
4645+ with self .subTest (offset_ns = offset_ns ):
4646+ created , relativeCreated = map (float , line .split ())
4647+ self .assertAlmostEqual (created , (start_ns + offset_ns ) / 1e9 , places = 6 )
46184648 # After PR gh-102412, precision (places) increases from 3 to 7
4619- self .assertAlmostEqual (record . relativeCreated , offset_ns / 1e6 , places = 7 )
4649+ self .assertAlmostEqual (relativeCreated , offset_ns / 1e6 , places = 7 )
46204650
46214651
46224652class TestBufferingFormatter (logging .BufferingFormatter ):
0 commit comments