99import platform
1010import re
1111import sys
12- import time
12+ import time as _time_module
1313import warnings
1414from pathlib import Path
1515from typing import TYPE_CHECKING , Any , Callable
@@ -74,6 +74,12 @@ class UnexpectedError(Exception):
7474 resource .setrlimit (resource .RLIMIT_AS , (memory_limit , memory_limit ))
7575
7676
77+ # Store references to original functions before any patching
78+ _ORIGINAL_TIME_TIME = _time_module .time
79+ _ORIGINAL_PERF_COUNTER = _time_module .perf_counter
80+ _ORIGINAL_TIME_SLEEP = _time_module .sleep
81+
82+
7783# Apply deterministic patches for reproducible test execution
7884def _apply_deterministic_patches () -> None :
7985 """Apply patches to make all sources of randomness deterministic."""
@@ -82,7 +88,7 @@ def _apply_deterministic_patches() -> None:
8288 import time
8389 import uuid
8490
85- # Store original functions
91+ # Store original functions (these are already saved globally above)
8692 _original_time = time .time
8793 _original_perf_counter = time .perf_counter
8894 _original_datetime_now = datetime .datetime .now
@@ -269,7 +275,7 @@ def pytest_runtestloop(self, session: Session) -> bool:
269275 if session .config .option .collectonly :
270276 return True
271277
272- start_time : float = time . time ()
278+ start_time : float = _ORIGINAL_TIME_TIME ()
273279 total_time : float = self ._get_total_time (session )
274280
275281 count : int = 0
@@ -296,7 +302,7 @@ def pytest_runtestloop(self, session: Session) -> bool:
296302 raise session .Interrupted (session .shouldstop )
297303 if self ._timed_out (session , start_time , count ):
298304 break # exit loop
299- time . sleep (self ._get_delay_time (session ))
305+ _ORIGINAL_TIME_SLEEP (self ._get_delay_time (session ))
300306 return True
301307
302308 def _clear_lru_caches (self , item : pytest .Item ) -> None :
@@ -395,7 +401,7 @@ def _timed_out(self, session: Session, start_time: float, count: int) -> bool:
395401 """
396402 return count >= session .config .option .codeflash_max_loops or (
397403 count >= session .config .option .codeflash_min_loops
398- and time . time () - start_time > self ._get_total_time (session )
404+ and _ORIGINAL_TIME_TIME () - start_time > self ._get_total_time (session )
399405 )
400406
401407 @pytest .fixture
0 commit comments