Skip to content

Commit

Permalink
Merge pull request #4180 from tybug/disable-hypothesis-events
Browse files Browse the repository at this point in the history
Disable line coverage events where not used
  • Loading branch information
Zac-HD authored Nov 17, 2024
2 parents 01d5d3e + 14d9b2b commit 97d63c0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

Hypothesis collects coverage information during the ``shrink`` and ``explain`` :ref:`phases <phases>` in order to show a more informative error message. On 3.12+, this uses :mod:`sys.monitoring`. This patch improves the performance of coverage collection on 3.12+ by disabling events we don't need.
13 changes: 8 additions & 5 deletions hypothesis-python/src/hypothesis/internal/scrutineer.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def trace(self, frame, event, arg):
if event == "call":
return self.trace
elif event == "line":
# manual inlining of self.trace_line for performance.
fname = frame.f_code.co_filename
if should_trace_file(fname):
current_location = (fname, frame.f_lineno)
Expand All @@ -87,10 +86,14 @@ def trace(self, frame, event, arg):

def trace_line(self, code: types.CodeType, line_number: int) -> None:
fname = code.co_filename
if should_trace_file(fname):
current_location = (fname, line_number)
self.branches.add((self._previous_location, current_location))
self._previous_location = current_location
if not should_trace_file(fname):
# this function is only called on 3.12+, but we want to avoid an
# assertion to that effect for performance.
return sys.monitoring.DISABLE # type: ignore

current_location = (fname, line_number)
self.branches.add((self._previous_location, current_location))
self._previous_location = current_location

def __enter__(self):
if not self._should_trace:
Expand Down

0 comments on commit 97d63c0

Please sign in to comment.