Skip to content

Commit 97d63c0

Browse files
authored
Merge pull request #4180 from tybug/disable-hypothesis-events
Disable line coverage events where not used
2 parents 01d5d3e + 14d9b2b commit 97d63c0

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

hypothesis-python/RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
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.

hypothesis-python/src/hypothesis/internal/scrutineer.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ def trace(self, frame, event, arg):
7676
if event == "call":
7777
return self.trace
7878
elif event == "line":
79-
# manual inlining of self.trace_line for performance.
8079
fname = frame.f_code.co_filename
8180
if should_trace_file(fname):
8281
current_location = (fname, frame.f_lineno)
@@ -87,10 +86,14 @@ def trace(self, frame, event, arg):
8786

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

9598
def __enter__(self):
9699
if not self._should_trace:

0 commit comments

Comments
 (0)