Skip to content

Commit

Permalink
Merge pull request #4057 from jobh/gc-import-perfcounter
Browse files Browse the repository at this point in the history
Store reference to time.perf_counter for gc accounting
  • Loading branch information
Zac-HD authored Jul 22, 2024
2 parents dc6b315 + a442a0c commit a967e75
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

This patch addresses the issue of hypothesis potentially accessing
mocked ``time.perf_counter`` during test execution (:issue:`4051`).
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ def find(self, condition: Callable[[T], bool]) -> T:
_gc_start = 0
_gc_cumulative_time = 0

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


def gc_cumulative_time() -> float:
global _gc_initialized
Expand All @@ -430,7 +434,7 @@ def gc_cumulative_time() -> float:
def gc_callback(phase, info):
global _gc_start, _gc_cumulative_time
try:
now = time.perf_counter()
now = _perf_counter()
if phase == "start":
_gc_start = now
elif phase == "stop" and _gc_start > 0:
Expand Down
2 changes: 2 additions & 0 deletions hypothesis-python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ def _patch(name, fn):
# non-determinism due to GC running at arbitrary times, we patch the GC observer
# to NOT increment time.

monkeypatch.setattr(junkdrawer, "_perf_counter", time)

if hasattr(gc, "callbacks"):
# ensure timer callback is added, then bracket it by freeze/unfreeze below
junkdrawer.gc_cumulative_time()
Expand Down

0 comments on commit a967e75

Please sign in to comment.