Skip to content

Commit 494aed4

Browse files
committed
Improve detection of sys.monitoring
1 parent 7ed359f commit 494aed4

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
Improve detection of sys.monitoring to avoid errors on GraalPy.

hypothesis-python/src/hypothesis/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ def run_engine(self):
14341434
# If we have not traced executions, warn about that now (but only when
14351435
# we'd expect to do so reliably, i.e. on CPython>=3.12)
14361436
if (
1437-
sys.version_info[:2] >= (3, 12)
1437+
hasattr(sys, "monitoring")
14381438
and not PYPY
14391439
and self._should_trace()
14401440
and not Tracer.can_trace()

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def should_trace_file(fname: str) -> bool:
4747
# tool_id = 1 is designated for coverage, but we intentionally choose a
4848
# non-reserved tool id so we can co-exist with coverage tools.
4949
MONITORING_TOOL_ID = 3
50-
if sys.version_info[:2] >= (3, 12):
50+
if hasattr(sys, "monitoring"):
5151
MONITORING_EVENTS = {sys.monitoring.events.LINE: "trace_line"}
5252

5353

@@ -69,13 +69,11 @@ def __init__(self, *, should_trace: bool) -> None:
6969

7070
@staticmethod
7171
def can_trace() -> bool:
72-
return (
73-
(sys.version_info[:2] < (3, 12) and sys.gettrace() is None)
74-
or (
75-
sys.version_info[:2] >= (3, 12)
76-
and sys.monitoring.get_tool(MONITORING_TOOL_ID) is None
77-
)
78-
) and not PYPY
72+
if PYPY:
73+
return False
74+
if hasattr(sys, "monitoring"):
75+
return sys.monitoring.get_tool(MONITORING_TOOL_ID) is None
76+
return sys.gettrace() is None
7977

8078
def trace(self, frame, event, arg):
8179
try:
@@ -107,7 +105,7 @@ def __enter__(self):
107105
if not self._should_trace:
108106
return self
109107

110-
if sys.version_info[:2] < (3, 12):
108+
if not hasattr(sys, "monitoring"):
111109
sys.settrace(self.trace)
112110
return self
113111

@@ -130,7 +128,7 @@ def __exit__(self, *args, **kwargs):
130128
if not self._should_trace:
131129
return
132130

133-
if sys.version_info[:2] < (3, 12):
131+
if not hasattr(sys, "monitoring"):
134132
sys.settrace(None)
135133
return
136134

0 commit comments

Comments
 (0)