From 8322b14253c4d1bb6ab70fe6742216438b8f61d1 Mon Sep 17 00:00:00 2001 From: Sergey Vasilyev Date: Sat, 3 Jan 2026 13:39:46 +0100 Subject: [PATCH] Use the original clock if not enabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now patch the event loop at creation — even if time compaction is not enabled. This is indeed required as we cannot patch the higher-scoped event loop later when the lower-scoped tests require it based on their individual options. However, this replaces the `loop.time()` function with our fake time, which starts at zero — even when the event loop is supposed to be "normal". Revert the behaviour of "normal" event loops to their original clock. This was originally caused and detected in a very rare case: `test_nonthreadsafe_indeed_fails()` шт Kopf, strictly in PyPy 3.11 (both setups) — but not шт PyPy 3.10, and not in any CPython. Specifically, the loop chronometer was showing the loop time advances for 0.4998 seconds despite there was a sync `time.sleep(0.5)` before any async activity happens. Since it is unlikely that there were any hidden or premature activities in the event loop, it was likely the issue with the time measurement of the replaced clock of the forcedly disactivated (but patched) event loop. Signed-off-by: Sergey Vasilyev --- looptime/_internal/loops.py | 5 ++++- tests/test_time_moves.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/looptime/_internal/loops.py b/looptime/_internal/loops.py index 7eb3e01..8b22412 100644 --- a/looptime/_internal/loops.py +++ b/looptime/_internal/loops.py @@ -157,7 +157,10 @@ def looptime_enabled(self) -> Iterator[None]: self.__enabled = old_enabled def time(self) -> float: - return self.__int2time(self.__now) + if self.__enabled: + return self.__int2time(self.__now) + else: + return super().time() def run_in_executor(self, executor: Any, func: Any, *args: Any) -> AnyFuture: # type: ignore future = super().run_in_executor(executor, func, *args) diff --git a/tests/test_time_moves.py b/tests/test_time_moves.py index 1a9d1f2..b17f7ab 100644 --- a/tests/test_time_moves.py +++ b/tests/test_time_moves.py @@ -33,7 +33,7 @@ def test_execution_takes_true_time_when_disabled(chronometer, looptime_loop): looptime_loop.setup_looptime(_enabled=False) with chronometer: looptime_loop.run_until_complete(asyncio.sleep(1)) - assert looptime_loop.time() == 1 + assert -1 < looptime_loop.time() - time.monotonic() < 1 # we know that asyncio uses this clock assert 1 <= chronometer.seconds < 1.1