Skip to content

Commit f5c866e

Browse files
committed
Make plugin aware of PEP 669
After this PEP there is now a formal way to determine if a debugger is active, which replaces sys.gettrace. Fixes #32
1 parent 7950087 commit f5c866e

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

alt_pytest_asyncio/plugin.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ def __init__(self, *, default_timeout: float) -> None:
117117
def use_default_timeout(self) -> None:
118118
self.set_timeout_seconds(self.timeout)
119119

120+
def debugger_enabled(self) -> bool:
121+
if sys.version_info >= (3, 12):
122+
return sys.monitoring.get_tool(sys.monitoring.DEBUGGER_ID) is not None
123+
else:
124+
# sys.gettrace is not a language feature and not guaranteed to be available
125+
# on all python implementations, so we see if it exists
126+
gettrace = getattr(sys, "gettrace", None)
127+
return gettrace is not None and gettrace() is not None
128+
120129
def set_timeout_seconds(self, timeout: float) -> None:
121130
if self._timeout:
122131
self._timeout.cancel()
@@ -130,10 +139,7 @@ def timeout_task(task: asyncio.Task[object] | None) -> None:
130139

131140
if task and not task.done():
132141
# If the debugger is active then don't cancel, so that debugging may continue
133-
# sys.gettrace is not a language feature and not guaranteed to be available
134-
# on all python implementations, so we see if it exists
135-
gettrace = getattr(sys, "gettrace", None)
136-
if gettrace is None or gettrace() is None:
142+
if not self.debugger_enabled():
137143
self.cancelled = True
138144
task.cancel()
139145

docs/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ generator fixtures.
2525
Changelog
2626
---------
2727

28+
.. _release-0.9.4:
29+
30+
0.9.4 - TBD
31+
* Make the plugin aware of PEP 669 (`#33 <https://github.com/delfick/alt-pytest-asyncio/pull/33>`_)
32+
2833
.. _release-0.9.3:
2934

3035
0.9.3 - 14 April 2025

0 commit comments

Comments
 (0)