Skip to content

Commit a967e75

Browse files
authored
Merge pull request #4057 from jobh/gc-import-perfcounter
Store reference to time.perf_counter for gc accounting
2 parents dc6b315 + a442a0c commit a967e75

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

hypothesis-python/RELEASE.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch addresses the issue of hypothesis potentially accessing
4+
mocked ``time.perf_counter`` during test execution (:issue:`4051`).

hypothesis-python/src/hypothesis/internal/conjecture/junkdrawer.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@ def find(self, condition: Callable[[T], bool]) -> T:
421421
_gc_start = 0
422422
_gc_cumulative_time = 0
423423

424+
# Since gc_callback potentially runs in test context, and perf_counter
425+
# might be monkeypatched, we store a reference to the real one.
426+
_perf_counter = time.perf_counter
427+
424428

425429
def gc_cumulative_time() -> float:
426430
global _gc_initialized
@@ -430,7 +434,7 @@ def gc_cumulative_time() -> float:
430434
def gc_callback(phase, info):
431435
global _gc_start, _gc_cumulative_time
432436
try:
433-
now = time.perf_counter()
437+
now = _perf_counter()
434438
if phase == "start":
435439
_gc_start = now
436440
elif phase == "stop" and _gc_start > 0:

hypothesis-python/tests/conftest.py

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ def _patch(name, fn):
116116
# non-determinism due to GC running at arbitrary times, we patch the GC observer
117117
# to NOT increment time.
118118

119+
monkeypatch.setattr(junkdrawer, "_perf_counter", time)
120+
119121
if hasattr(gc, "callbacks"):
120122
# ensure timer callback is added, then bracket it by freeze/unfreeze below
121123
junkdrawer.gc_cumulative_time()

0 commit comments

Comments
 (0)