| 1 |
# *********************************************************** |
| 2 |
# ******* WARNING: AUTOGENERATED! ALL EDITS WILL BE LOST ****** |
| 3 |
# ************************************************************* |
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
from ._ki import enable_ki_protection |
| 9 |
from ._run import GLOBAL_RUN_CONTEXT |
| 10 |
|
| 11 |
if TYPE_CHECKING: |
| 12 |
from ._instrumentation import Instrument |
| 13 |
|
| 14 |
__all__ = ["add_instrument", "remove_instrument"] |
| 15 |
|
| 16 |
|
| 17 |
@enable_ki_protection |
| 18 |
def add_instrument(instrument: Instrument) -> None: |
| 19 |
"""Start instrumenting the current run loop with the given instrument. |
| 20 |
|
| 21 |
Args: |
| 22 |
instrument (trio.abc.Instrument): The instrument to activate. |
| 23 |
|
| 24 |
If ``instrument`` is already active, does nothing. |
| 25 |
|
| 26 |
""" |
| 27 |
try: |
| 28 |
return GLOBAL_RUN_CONTEXT.runner.instruments.add_instrument(instrument) |
| 29 |
except AttributeError: |
| 30 |
raise RuntimeError("must be called from async context") from None |
| 31 |
|
| 32 |
|
| 33 |
@enable_ki_protection |
| 34 |
def remove_instrument(instrument: Instrument) -> None: |
| 35 |
"""Stop instrumenting the current run loop with the given instrument. |
| 36 |
|
| 37 |
Args: |
| 38 |
instrument (trio.abc.Instrument): The instrument to de-activate. |
| 39 |
|
| 40 |
Raises: |
| 41 |
KeyError: if the instrument is not currently active. This could |
| 42 |
occur either because you never added it, or because you added it |
| 43 |
and then it raised an unhandled exception and was automatically |
| 44 |
deactivated. |
| 45 |
|
| 46 |
""" |
| 47 |
try: |
| 48 |
return GLOBAL_RUN_CONTEXT.runner.instruments.remove_instrument(instrument) |
| 49 |
except AttributeError: |
| 50 |
raise RuntimeError("must be called from async context") from None |