| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Generic, TypeVar, cast |
| 4 |
|
| 5 |
# Runvar implementations |
| 6 |
import attrs |
| 7 |
|
| 8 |
from .._util import NoPublicConstructor, final |
| 9 |
from . import _run |
| 10 |
|
| 11 |
T = TypeVar("T") |
| 12 |
|
| 13 |
|
| 14 |
@final |
| 15 |
class _NoValue: ... |
| 16 |
|
| 17 |
|
| 18 |
@final |
| 19 |
@attrs.define(eq=False) |
| 20 |
class RunVarToken(Generic[T], metaclass=NoPublicConstructor): |
| 21 |
_var: RunVar[T] |
| 22 |
previous_value: T | type[_NoValue] = _NoValue |
| 23 |
redeemed: bool = attrs.field(default=False, init=False) |
| 24 |
|
| 25 |
@classmethod |
| 26 |
def _empty(cls, var: RunVar[T]) -> RunVarToken[T]: |
| 27 |
return cls._create(var) |
| 28 |
|
| 29 |
|
| 30 |
@final |
| 31 |
@attrs.define(eq=False, repr=False) |
| 32 |
class RunVar(Generic[T]): |
| 33 |
"""The run-local variant of a context variable. |
| 34 |
|
| 35 |
:class:`RunVar` objects are similar to context variable objects, |
| 36 |
except that they are shared across a single call to :func:`trio.run` |
| 37 |
rather than a single task. |
| 38 |
|
| 39 |
""" |
| 40 |
|
| 41 |
_name: str = attrs.field(alias="name") |
| 42 |
_default: T | type[_NoValue] = attrs.field(default=_NoValue, alias="default") |
| 43 |
|
| 44 |
def get(self, default: T | type[_NoValue] = _NoValue) -> T: |
| 45 |
"""Gets the value of this :class:`RunVar` for the current run call.""" |
| 46 |
try: |
| 47 |
return cast("T", _run.GLOBAL_RUN_CONTEXT.runner._locals[self]) |
| 48 |
except AttributeError: |
| 49 |
raise RuntimeError("Cannot be used outside of a run context") from None |
| 50 |
except KeyError: |
| 51 |
# contextvars consistency |
| 52 |
if default is not _NoValue: |
| 53 |
return default |
| 54 |
|
| 55 |
if self._default is not _NoValue: |
| 56 |
return self._default |
| 57 |
|
| 58 |
raise LookupError(self) from None |
| 59 |
|
| 60 |
def set(self, value: T) -> RunVarToken[T]: |
| 61 |
"""Sets the value of this :class:`RunVar` for this current run |
| 62 |
call. |
| 63 |
|
| 64 |
""" |
| 65 |
try: |
| 66 |
old_value = self.get() |
| 67 |
except LookupError: |
| 68 |
token = RunVarToken._empty(self) |
| 69 |
else: |
| 70 |
token = RunVarToken[T]._create(self, old_value) |
| 71 |
|
| 72 |
# This can't fail, because if we weren't in Trio context then the |
| 73 |
# get() above would have failed. |
| 74 |
_run.GLOBAL_RUN_CONTEXT.runner._locals[self] = value |
| 75 |
return token |
| 76 |
|
| 77 |
def reset(self, token: RunVarToken[T]) -> None: |
| 78 |
"""Resets the value of this :class:`RunVar` to what it was |
| 79 |
previously specified by the token. |
| 80 |
|
| 81 |
""" |
| 82 |
if token is None: |
| 83 |
raise TypeError("token must not be none") |
| 84 |
|
| 85 |
if token.redeemed: |
| 86 |
raise ValueError("token has already been used") |
| 87 |
|
| 88 |
if token._var is not self: |
| 89 |
raise ValueError("token is not for us") |
| 90 |
|
| 91 |
previous = token.previous_value |
| 92 |
try: |
| 93 |
if previous is _NoValue: |
| 94 |
_run.GLOBAL_RUN_CONTEXT.runner._locals.pop(self) |
| 95 |
else: |
| 96 |
_run.GLOBAL_RUN_CONTEXT.runner._locals[self] = previous |
| 97 |
except AttributeError: |
| 98 |
raise RuntimeError("Cannot be used outside of a run context") from None |
| 99 |
|
| 100 |
token.redeemed = True |
| 101 |
|
| 102 |
def __repr__(self) -> str: |
| 103 |
return f"<RunVar name={self._name!r}>" |