Datasets:
Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- data/train/labels/187682.txt +0 -0
- data/train/labels/188713.txt +0 -0
- data/train/labels/265710.txt +1 -0
- data/train/labels/387618.txt +0 -0
- data/train/labels/389197.txt +0 -0
- data/train/labels/389865.txt +0 -0
- data/train/labels/390813.txt +0 -0
- data/train/labels/391422.txt +0 -0
- data/train/labels/391423.txt +0 -0
- data/train/labels/397448.txt +0 -0
- data/train/labels/407076.txt +0 -0
- data/train/labels/407784.txt +0 -0
- data/train/labels/43857.txt +0 -0
- data/train/labels/43871.txt +0 -0
- data/train/labels/44812.txt +0 -0
- data/train/labels/51233.txt +0 -0
- data/train/labels/52049.txt +0 -0
- env/lib/python3.13/site-packages/anyio/__pycache__/__init__.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/__pycache__/from_thread.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/__pycache__/functools.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/__pycache__/lowlevel.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/__pycache__/pytest_plugin.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/__pycache__/to_interpreter.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/__pycache__/to_process.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/__pycache__/to_thread.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_backends/__init__.py +0 -0
- env/lib/python3.13/site-packages/anyio/_backends/__pycache__/__init__.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_backends/__pycache__/_trio.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_backends/_asyncio.py +0 -0
- env/lib/python3.13/site-packages/anyio/_backends/_trio.py +1346 -0
- env/lib/python3.13/site-packages/anyio/_core/__init__.py +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/__init__.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_asyncio_selector_thread.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_contextmanagers.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_eventloop.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_exceptions.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_fileio.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_resources.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_signals.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_sockets.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_streams.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_subprocesses.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_synchronization.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_tasks.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_tempfile.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_testing.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/__pycache__/_typedattr.cpython-313.pyc +0 -0
- env/lib/python3.13/site-packages/anyio/_core/_asyncio_selector_thread.py +167 -0
- env/lib/python3.13/site-packages/anyio/_core/_contextmanagers.py +200 -0
- env/lib/python3.13/site-packages/anyio/_core/_eventloop.py +234 -0
data/train/labels/187682.txt
ADDED
|
File without changes
|
data/train/labels/188713.txt
ADDED
|
File without changes
|
data/train/labels/265710.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
0 0.402239 0.486431 0.012081 0.0282257
|
data/train/labels/387618.txt
ADDED
|
File without changes
|
data/train/labels/389197.txt
ADDED
|
File without changes
|
data/train/labels/389865.txt
ADDED
|
File without changes
|
data/train/labels/390813.txt
ADDED
|
File without changes
|
data/train/labels/391422.txt
ADDED
|
File without changes
|
data/train/labels/391423.txt
ADDED
|
File without changes
|
data/train/labels/397448.txt
ADDED
|
File without changes
|
data/train/labels/407076.txt
ADDED
|
File without changes
|
data/train/labels/407784.txt
ADDED
|
File without changes
|
data/train/labels/43857.txt
ADDED
|
File without changes
|
data/train/labels/43871.txt
ADDED
|
File without changes
|
data/train/labels/44812.txt
ADDED
|
File without changes
|
data/train/labels/51233.txt
ADDED
|
File without changes
|
data/train/labels/52049.txt
ADDED
|
File without changes
|
env/lib/python3.13/site-packages/anyio/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (4.65 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/__pycache__/from_thread.cpython-313.pyc
ADDED
|
Binary file (25.5 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/__pycache__/functools.cpython-313.pyc
ADDED
|
Binary file (15.8 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/__pycache__/lowlevel.cpython-313.pyc
ADDED
|
Binary file (8.01 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/__pycache__/pytest_plugin.cpython-313.pyc
ADDED
|
Binary file (14.7 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/__pycache__/to_interpreter.cpython-313.pyc
ADDED
|
Binary file (10.4 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/__pycache__/to_process.cpython-313.pyc
ADDED
|
Binary file (12.1 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/__pycache__/to_thread.cpython-313.pyc
ADDED
|
Binary file (3.09 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_backends/__init__.py
ADDED
|
File without changes
|
env/lib/python3.13/site-packages/anyio/_backends/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (197 Bytes). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_backends/__pycache__/_trio.cpython-313.pyc
ADDED
|
Binary file (73.2 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_backends/_asyncio.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
env/lib/python3.13/site-packages/anyio/_backends/_trio.py
ADDED
|
@@ -0,0 +1,1346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import array
|
| 4 |
+
import math
|
| 5 |
+
import os
|
| 6 |
+
import socket
|
| 7 |
+
import sys
|
| 8 |
+
import types
|
| 9 |
+
import weakref
|
| 10 |
+
from collections.abc import (
|
| 11 |
+
AsyncGenerator,
|
| 12 |
+
AsyncIterator,
|
| 13 |
+
Awaitable,
|
| 14 |
+
Callable,
|
| 15 |
+
Collection,
|
| 16 |
+
Coroutine,
|
| 17 |
+
Iterable,
|
| 18 |
+
Sequence,
|
| 19 |
+
)
|
| 20 |
+
from contextlib import AbstractContextManager
|
| 21 |
+
from dataclasses import dataclass
|
| 22 |
+
from io import IOBase
|
| 23 |
+
from os import PathLike
|
| 24 |
+
from signal import Signals
|
| 25 |
+
from socket import AddressFamily, SocketKind
|
| 26 |
+
from types import TracebackType
|
| 27 |
+
from typing import (
|
| 28 |
+
IO,
|
| 29 |
+
TYPE_CHECKING,
|
| 30 |
+
Any,
|
| 31 |
+
Generic,
|
| 32 |
+
NoReturn,
|
| 33 |
+
TypeVar,
|
| 34 |
+
cast,
|
| 35 |
+
overload,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
import trio.from_thread
|
| 39 |
+
import trio.lowlevel
|
| 40 |
+
from outcome import Error, Outcome, Value
|
| 41 |
+
from trio.lowlevel import (
|
| 42 |
+
current_root_task,
|
| 43 |
+
current_task,
|
| 44 |
+
notify_closing,
|
| 45 |
+
wait_readable,
|
| 46 |
+
wait_writable,
|
| 47 |
+
)
|
| 48 |
+
from trio.socket import SocketType as TrioSocketType
|
| 49 |
+
from trio.to_thread import run_sync
|
| 50 |
+
|
| 51 |
+
from .. import (
|
| 52 |
+
CapacityLimiterStatistics,
|
| 53 |
+
EventStatistics,
|
| 54 |
+
LockStatistics,
|
| 55 |
+
RunFinishedError,
|
| 56 |
+
TaskInfo,
|
| 57 |
+
WouldBlock,
|
| 58 |
+
abc,
|
| 59 |
+
)
|
| 60 |
+
from .._core._eventloop import claim_worker_thread
|
| 61 |
+
from .._core._exceptions import (
|
| 62 |
+
BrokenResourceError,
|
| 63 |
+
BusyResourceError,
|
| 64 |
+
ClosedResourceError,
|
| 65 |
+
EndOfStream,
|
| 66 |
+
)
|
| 67 |
+
from .._core._sockets import convert_ipv6_sockaddr
|
| 68 |
+
from .._core._streams import create_memory_object_stream
|
| 69 |
+
from .._core._synchronization import (
|
| 70 |
+
CapacityLimiter as BaseCapacityLimiter,
|
| 71 |
+
)
|
| 72 |
+
from .._core._synchronization import Event as BaseEvent
|
| 73 |
+
from .._core._synchronization import Lock as BaseLock
|
| 74 |
+
from .._core._synchronization import (
|
| 75 |
+
ResourceGuard,
|
| 76 |
+
SemaphoreStatistics,
|
| 77 |
+
)
|
| 78 |
+
from .._core._synchronization import Semaphore as BaseSemaphore
|
| 79 |
+
from .._core._tasks import CancelScope as BaseCancelScope
|
| 80 |
+
from ..abc import IPSockAddrType, UDPPacketType, UNIXDatagramPacketType
|
| 81 |
+
from ..abc._eventloop import AsyncBackend, StrOrBytesPath
|
| 82 |
+
from ..streams.memory import MemoryObjectSendStream
|
| 83 |
+
|
| 84 |
+
if TYPE_CHECKING:
|
| 85 |
+
from _typeshed import FileDescriptorLike
|
| 86 |
+
|
| 87 |
+
if sys.version_info >= (3, 10):
|
| 88 |
+
from typing import ParamSpec
|
| 89 |
+
else:
|
| 90 |
+
from typing_extensions import ParamSpec
|
| 91 |
+
|
| 92 |
+
if sys.version_info >= (3, 11):
|
| 93 |
+
from typing import TypeVarTuple, Unpack
|
| 94 |
+
else:
|
| 95 |
+
from exceptiongroup import BaseExceptionGroup
|
| 96 |
+
from typing_extensions import TypeVarTuple, Unpack
|
| 97 |
+
|
| 98 |
+
T = TypeVar("T")
|
| 99 |
+
T_Retval = TypeVar("T_Retval")
|
| 100 |
+
T_SockAddr = TypeVar("T_SockAddr", str, IPSockAddrType)
|
| 101 |
+
PosArgsT = TypeVarTuple("PosArgsT")
|
| 102 |
+
P = ParamSpec("P")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
#
|
| 106 |
+
# Event loop
|
| 107 |
+
#
|
| 108 |
+
|
| 109 |
+
RunVar = trio.lowlevel.RunVar
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
#
|
| 113 |
+
# Timeouts and cancellation
|
| 114 |
+
#
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
class CancelScope(BaseCancelScope):
|
| 118 |
+
def __new__(
|
| 119 |
+
cls, original: trio.CancelScope | None = None, **kwargs: object
|
| 120 |
+
) -> CancelScope:
|
| 121 |
+
return object.__new__(cls)
|
| 122 |
+
|
| 123 |
+
def __init__(self, original: trio.CancelScope | None = None, **kwargs: Any) -> None:
|
| 124 |
+
self.__original = original or trio.CancelScope(**kwargs)
|
| 125 |
+
|
| 126 |
+
def __enter__(self) -> CancelScope:
|
| 127 |
+
self.__original.__enter__()
|
| 128 |
+
return self
|
| 129 |
+
|
| 130 |
+
def __exit__(
|
| 131 |
+
self,
|
| 132 |
+
exc_type: type[BaseException] | None,
|
| 133 |
+
exc_val: BaseException | None,
|
| 134 |
+
exc_tb: TracebackType | None,
|
| 135 |
+
) -> bool:
|
| 136 |
+
return self.__original.__exit__(exc_type, exc_val, exc_tb)
|
| 137 |
+
|
| 138 |
+
def cancel(self, reason: str | None = None) -> None:
|
| 139 |
+
self.__original.cancel(reason)
|
| 140 |
+
|
| 141 |
+
@property
|
| 142 |
+
def deadline(self) -> float:
|
| 143 |
+
return self.__original.deadline
|
| 144 |
+
|
| 145 |
+
@deadline.setter
|
| 146 |
+
def deadline(self, value: float) -> None:
|
| 147 |
+
self.__original.deadline = value
|
| 148 |
+
|
| 149 |
+
@property
|
| 150 |
+
def cancel_called(self) -> bool:
|
| 151 |
+
return self.__original.cancel_called
|
| 152 |
+
|
| 153 |
+
@property
|
| 154 |
+
def cancelled_caught(self) -> bool:
|
| 155 |
+
return self.__original.cancelled_caught
|
| 156 |
+
|
| 157 |
+
@property
|
| 158 |
+
def shield(self) -> bool:
|
| 159 |
+
return self.__original.shield
|
| 160 |
+
|
| 161 |
+
@shield.setter
|
| 162 |
+
def shield(self, value: bool) -> None:
|
| 163 |
+
self.__original.shield = value
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
#
|
| 167 |
+
# Task groups
|
| 168 |
+
#
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
class TaskGroup(abc.TaskGroup):
|
| 172 |
+
def __init__(self) -> None:
|
| 173 |
+
self._active = False
|
| 174 |
+
self._nursery_manager = trio.open_nursery(strict_exception_groups=True)
|
| 175 |
+
self.cancel_scope = None # type: ignore[assignment]
|
| 176 |
+
|
| 177 |
+
async def __aenter__(self) -> TaskGroup:
|
| 178 |
+
self._active = True
|
| 179 |
+
self._nursery = await self._nursery_manager.__aenter__()
|
| 180 |
+
self.cancel_scope = CancelScope(self._nursery.cancel_scope)
|
| 181 |
+
return self
|
| 182 |
+
|
| 183 |
+
async def __aexit__(
|
| 184 |
+
self,
|
| 185 |
+
exc_type: type[BaseException] | None,
|
| 186 |
+
exc_val: BaseException | None,
|
| 187 |
+
exc_tb: TracebackType | None,
|
| 188 |
+
) -> bool:
|
| 189 |
+
try:
|
| 190 |
+
# trio.Nursery.__exit__ returns bool; .open_nursery has wrong type
|
| 191 |
+
return await self._nursery_manager.__aexit__(exc_type, exc_val, exc_tb) # type: ignore[return-value]
|
| 192 |
+
except BaseExceptionGroup as exc:
|
| 193 |
+
if not exc.split(trio.Cancelled)[1]:
|
| 194 |
+
raise trio.Cancelled._create() from exc
|
| 195 |
+
|
| 196 |
+
raise
|
| 197 |
+
finally:
|
| 198 |
+
del exc_val, exc_tb
|
| 199 |
+
self._active = False
|
| 200 |
+
|
| 201 |
+
def start_soon(
|
| 202 |
+
self,
|
| 203 |
+
func: Callable[[Unpack[PosArgsT]], Awaitable[Any]],
|
| 204 |
+
*args: Unpack[PosArgsT],
|
| 205 |
+
name: object = None,
|
| 206 |
+
) -> None:
|
| 207 |
+
if not self._active:
|
| 208 |
+
raise RuntimeError(
|
| 209 |
+
"This task group is not active; no new tasks can be started."
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
self._nursery.start_soon(func, *args, name=name)
|
| 213 |
+
|
| 214 |
+
async def start(
|
| 215 |
+
self, func: Callable[..., Awaitable[Any]], *args: object, name: object = None
|
| 216 |
+
) -> Any:
|
| 217 |
+
if not self._active:
|
| 218 |
+
raise RuntimeError(
|
| 219 |
+
"This task group is not active; no new tasks can be started."
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
return await self._nursery.start(func, *args, name=name)
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
#
|
| 226 |
+
# Subprocesses
|
| 227 |
+
#
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
@dataclass(eq=False)
|
| 231 |
+
class ReceiveStreamWrapper(abc.ByteReceiveStream):
|
| 232 |
+
_stream: trio.abc.ReceiveStream
|
| 233 |
+
|
| 234 |
+
async def receive(self, max_bytes: int | None = None) -> bytes:
|
| 235 |
+
try:
|
| 236 |
+
data = await self._stream.receive_some(max_bytes)
|
| 237 |
+
except trio.ClosedResourceError as exc:
|
| 238 |
+
raise ClosedResourceError from exc.__cause__
|
| 239 |
+
except trio.BrokenResourceError as exc:
|
| 240 |
+
raise BrokenResourceError from exc.__cause__
|
| 241 |
+
|
| 242 |
+
if data:
|
| 243 |
+
return bytes(data)
|
| 244 |
+
else:
|
| 245 |
+
raise EndOfStream
|
| 246 |
+
|
| 247 |
+
async def aclose(self) -> None:
|
| 248 |
+
await self._stream.aclose()
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
@dataclass(eq=False)
|
| 252 |
+
class SendStreamWrapper(abc.ByteSendStream):
|
| 253 |
+
_stream: trio.abc.SendStream
|
| 254 |
+
|
| 255 |
+
async def send(self, item: bytes) -> None:
|
| 256 |
+
try:
|
| 257 |
+
await self._stream.send_all(item)
|
| 258 |
+
except trio.ClosedResourceError as exc:
|
| 259 |
+
raise ClosedResourceError from exc.__cause__
|
| 260 |
+
except trio.BrokenResourceError as exc:
|
| 261 |
+
raise BrokenResourceError from exc.__cause__
|
| 262 |
+
|
| 263 |
+
async def aclose(self) -> None:
|
| 264 |
+
await self._stream.aclose()
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
@dataclass(eq=False)
|
| 268 |
+
class Process(abc.Process):
|
| 269 |
+
_process: trio.Process
|
| 270 |
+
_stdin: abc.ByteSendStream | None
|
| 271 |
+
_stdout: abc.ByteReceiveStream | None
|
| 272 |
+
_stderr: abc.ByteReceiveStream | None
|
| 273 |
+
|
| 274 |
+
async def aclose(self) -> None:
|
| 275 |
+
with CancelScope(shield=True):
|
| 276 |
+
if self._stdin:
|
| 277 |
+
await self._stdin.aclose()
|
| 278 |
+
if self._stdout:
|
| 279 |
+
await self._stdout.aclose()
|
| 280 |
+
if self._stderr:
|
| 281 |
+
await self._stderr.aclose()
|
| 282 |
+
|
| 283 |
+
try:
|
| 284 |
+
await self.wait()
|
| 285 |
+
except BaseException:
|
| 286 |
+
self.kill()
|
| 287 |
+
with CancelScope(shield=True):
|
| 288 |
+
await self.wait()
|
| 289 |
+
raise
|
| 290 |
+
|
| 291 |
+
async def wait(self) -> int:
|
| 292 |
+
return await self._process.wait()
|
| 293 |
+
|
| 294 |
+
def terminate(self) -> None:
|
| 295 |
+
self._process.terminate()
|
| 296 |
+
|
| 297 |
+
def kill(self) -> None:
|
| 298 |
+
self._process.kill()
|
| 299 |
+
|
| 300 |
+
def send_signal(self, signal: Signals) -> None:
|
| 301 |
+
self._process.send_signal(signal)
|
| 302 |
+
|
| 303 |
+
@property
|
| 304 |
+
def pid(self) -> int:
|
| 305 |
+
return self._process.pid
|
| 306 |
+
|
| 307 |
+
@property
|
| 308 |
+
def returncode(self) -> int | None:
|
| 309 |
+
return self._process.returncode
|
| 310 |
+
|
| 311 |
+
@property
|
| 312 |
+
def stdin(self) -> abc.ByteSendStream | None:
|
| 313 |
+
return self._stdin
|
| 314 |
+
|
| 315 |
+
@property
|
| 316 |
+
def stdout(self) -> abc.ByteReceiveStream | None:
|
| 317 |
+
return self._stdout
|
| 318 |
+
|
| 319 |
+
@property
|
| 320 |
+
def stderr(self) -> abc.ByteReceiveStream | None:
|
| 321 |
+
return self._stderr
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
class _ProcessPoolShutdownInstrument(trio.abc.Instrument):
|
| 325 |
+
def after_run(self) -> None:
|
| 326 |
+
super().after_run()
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
current_default_worker_process_limiter: trio.lowlevel.RunVar = RunVar(
|
| 330 |
+
"current_default_worker_process_limiter"
|
| 331 |
+
)
|
| 332 |
+
|
| 333 |
+
|
| 334 |
+
async def _shutdown_process_pool(workers: set[abc.Process]) -> None:
|
| 335 |
+
try:
|
| 336 |
+
await trio.sleep(math.inf)
|
| 337 |
+
except trio.Cancelled:
|
| 338 |
+
for process in workers:
|
| 339 |
+
if process.returncode is None:
|
| 340 |
+
process.kill()
|
| 341 |
+
|
| 342 |
+
with CancelScope(shield=True):
|
| 343 |
+
for process in workers:
|
| 344 |
+
await process.aclose()
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
#
|
| 348 |
+
# Sockets and networking
|
| 349 |
+
#
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
class _TrioSocketMixin(Generic[T_SockAddr]):
|
| 353 |
+
def __init__(self, trio_socket: TrioSocketType) -> None:
|
| 354 |
+
self._trio_socket = trio_socket
|
| 355 |
+
self._closed = False
|
| 356 |
+
|
| 357 |
+
def _check_closed(self) -> None:
|
| 358 |
+
if self._closed:
|
| 359 |
+
raise ClosedResourceError
|
| 360 |
+
if self._trio_socket.fileno() < 0:
|
| 361 |
+
raise BrokenResourceError
|
| 362 |
+
|
| 363 |
+
@property
|
| 364 |
+
def _raw_socket(self) -> socket.socket:
|
| 365 |
+
return self._trio_socket._sock # type: ignore[attr-defined]
|
| 366 |
+
|
| 367 |
+
async def aclose(self) -> None:
|
| 368 |
+
if self._trio_socket.fileno() >= 0:
|
| 369 |
+
self._closed = True
|
| 370 |
+
self._trio_socket.close()
|
| 371 |
+
|
| 372 |
+
def _convert_socket_error(self, exc: BaseException) -> NoReturn:
|
| 373 |
+
if isinstance(exc, trio.ClosedResourceError):
|
| 374 |
+
raise ClosedResourceError from exc
|
| 375 |
+
elif self._trio_socket.fileno() < 0 and self._closed:
|
| 376 |
+
raise ClosedResourceError from None
|
| 377 |
+
elif isinstance(exc, OSError):
|
| 378 |
+
raise BrokenResourceError from exc
|
| 379 |
+
else:
|
| 380 |
+
raise exc
|
| 381 |
+
|
| 382 |
+
|
| 383 |
+
class SocketStream(_TrioSocketMixin, abc.SocketStream):
|
| 384 |
+
def __init__(self, trio_socket: TrioSocketType) -> None:
|
| 385 |
+
super().__init__(trio_socket)
|
| 386 |
+
self._receive_guard = ResourceGuard("reading from")
|
| 387 |
+
self._send_guard = ResourceGuard("writing to")
|
| 388 |
+
|
| 389 |
+
async def receive(self, max_bytes: int = 65536) -> bytes:
|
| 390 |
+
with self._receive_guard:
|
| 391 |
+
try:
|
| 392 |
+
data = await self._trio_socket.recv(max_bytes)
|
| 393 |
+
except BaseException as exc:
|
| 394 |
+
self._convert_socket_error(exc)
|
| 395 |
+
|
| 396 |
+
if data:
|
| 397 |
+
return data
|
| 398 |
+
else:
|
| 399 |
+
raise EndOfStream
|
| 400 |
+
|
| 401 |
+
async def send(self, item: bytes) -> None:
|
| 402 |
+
with self._send_guard:
|
| 403 |
+
view = memoryview(item)
|
| 404 |
+
while view:
|
| 405 |
+
try:
|
| 406 |
+
bytes_sent = await self._trio_socket.send(view)
|
| 407 |
+
except BaseException as exc:
|
| 408 |
+
self._convert_socket_error(exc)
|
| 409 |
+
|
| 410 |
+
view = view[bytes_sent:]
|
| 411 |
+
|
| 412 |
+
async def send_eof(self) -> None:
|
| 413 |
+
self._trio_socket.shutdown(socket.SHUT_WR)
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
class UNIXSocketStream(SocketStream, abc.UNIXSocketStream):
|
| 417 |
+
async def receive_fds(self, msglen: int, maxfds: int) -> tuple[bytes, list[int]]:
|
| 418 |
+
if not isinstance(msglen, int) or msglen < 0:
|
| 419 |
+
raise ValueError("msglen must be a non-negative integer")
|
| 420 |
+
if not isinstance(maxfds, int) or maxfds < 1:
|
| 421 |
+
raise ValueError("maxfds must be a positive integer")
|
| 422 |
+
|
| 423 |
+
fds = array.array("i")
|
| 424 |
+
await trio.lowlevel.checkpoint()
|
| 425 |
+
with self._receive_guard:
|
| 426 |
+
while True:
|
| 427 |
+
try:
|
| 428 |
+
message, ancdata, flags, addr = await self._trio_socket.recvmsg(
|
| 429 |
+
msglen, socket.CMSG_LEN(maxfds * fds.itemsize)
|
| 430 |
+
)
|
| 431 |
+
except BaseException as exc:
|
| 432 |
+
self._convert_socket_error(exc)
|
| 433 |
+
else:
|
| 434 |
+
if not message and not ancdata:
|
| 435 |
+
raise EndOfStream
|
| 436 |
+
|
| 437 |
+
break
|
| 438 |
+
|
| 439 |
+
for cmsg_level, cmsg_type, cmsg_data in ancdata:
|
| 440 |
+
if cmsg_level != socket.SOL_SOCKET or cmsg_type != socket.SCM_RIGHTS:
|
| 441 |
+
raise RuntimeError(
|
| 442 |
+
f"Received unexpected ancillary data; message = {message!r}, "
|
| 443 |
+
f"cmsg_level = {cmsg_level}, cmsg_type = {cmsg_type}"
|
| 444 |
+
)
|
| 445 |
+
|
| 446 |
+
fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
|
| 447 |
+
|
| 448 |
+
return message, list(fds)
|
| 449 |
+
|
| 450 |
+
async def send_fds(self, message: bytes, fds: Collection[int | IOBase]) -> None:
|
| 451 |
+
if not message:
|
| 452 |
+
raise ValueError("message must not be empty")
|
| 453 |
+
if not fds:
|
| 454 |
+
raise ValueError("fds must not be empty")
|
| 455 |
+
|
| 456 |
+
filenos: list[int] = []
|
| 457 |
+
for fd in fds:
|
| 458 |
+
if isinstance(fd, int):
|
| 459 |
+
filenos.append(fd)
|
| 460 |
+
elif isinstance(fd, IOBase):
|
| 461 |
+
filenos.append(fd.fileno())
|
| 462 |
+
|
| 463 |
+
fdarray = array.array("i", filenos)
|
| 464 |
+
await trio.lowlevel.checkpoint()
|
| 465 |
+
with self._send_guard:
|
| 466 |
+
while True:
|
| 467 |
+
try:
|
| 468 |
+
await self._trio_socket.sendmsg(
|
| 469 |
+
[message],
|
| 470 |
+
[
|
| 471 |
+
(
|
| 472 |
+
socket.SOL_SOCKET,
|
| 473 |
+
socket.SCM_RIGHTS,
|
| 474 |
+
fdarray,
|
| 475 |
+
)
|
| 476 |
+
],
|
| 477 |
+
)
|
| 478 |
+
break
|
| 479 |
+
except BaseException as exc:
|
| 480 |
+
self._convert_socket_error(exc)
|
| 481 |
+
|
| 482 |
+
|
| 483 |
+
class TCPSocketListener(_TrioSocketMixin, abc.SocketListener):
|
| 484 |
+
def __init__(self, raw_socket: socket.socket):
|
| 485 |
+
super().__init__(trio.socket.from_stdlib_socket(raw_socket))
|
| 486 |
+
self._accept_guard = ResourceGuard("accepting connections from")
|
| 487 |
+
|
| 488 |
+
async def accept(self) -> SocketStream:
|
| 489 |
+
with self._accept_guard:
|
| 490 |
+
try:
|
| 491 |
+
trio_socket, _addr = await self._trio_socket.accept()
|
| 492 |
+
except BaseException as exc:
|
| 493 |
+
self._convert_socket_error(exc)
|
| 494 |
+
|
| 495 |
+
trio_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
| 496 |
+
return SocketStream(trio_socket)
|
| 497 |
+
|
| 498 |
+
|
| 499 |
+
class UNIXSocketListener(_TrioSocketMixin, abc.SocketListener):
|
| 500 |
+
def __init__(self, raw_socket: socket.socket):
|
| 501 |
+
super().__init__(trio.socket.from_stdlib_socket(raw_socket))
|
| 502 |
+
self._accept_guard = ResourceGuard("accepting connections from")
|
| 503 |
+
|
| 504 |
+
async def accept(self) -> UNIXSocketStream:
|
| 505 |
+
with self._accept_guard:
|
| 506 |
+
try:
|
| 507 |
+
trio_socket, _addr = await self._trio_socket.accept()
|
| 508 |
+
except BaseException as exc:
|
| 509 |
+
self._convert_socket_error(exc)
|
| 510 |
+
|
| 511 |
+
return UNIXSocketStream(trio_socket)
|
| 512 |
+
|
| 513 |
+
|
| 514 |
+
class UDPSocket(_TrioSocketMixin[IPSockAddrType], abc.UDPSocket):
|
| 515 |
+
def __init__(self, trio_socket: TrioSocketType) -> None:
|
| 516 |
+
super().__init__(trio_socket)
|
| 517 |
+
self._receive_guard = ResourceGuard("reading from")
|
| 518 |
+
self._send_guard = ResourceGuard("writing to")
|
| 519 |
+
|
| 520 |
+
async def receive(self) -> tuple[bytes, IPSockAddrType]:
|
| 521 |
+
with self._receive_guard:
|
| 522 |
+
try:
|
| 523 |
+
data, addr = await self._trio_socket.recvfrom(65536)
|
| 524 |
+
return data, convert_ipv6_sockaddr(addr)
|
| 525 |
+
except BaseException as exc:
|
| 526 |
+
self._convert_socket_error(exc)
|
| 527 |
+
|
| 528 |
+
async def send(self, item: UDPPacketType) -> None:
|
| 529 |
+
with self._send_guard:
|
| 530 |
+
try:
|
| 531 |
+
await self._trio_socket.sendto(*item)
|
| 532 |
+
except BaseException as exc:
|
| 533 |
+
self._convert_socket_error(exc)
|
| 534 |
+
|
| 535 |
+
|
| 536 |
+
class ConnectedUDPSocket(_TrioSocketMixin[IPSockAddrType], abc.ConnectedUDPSocket):
|
| 537 |
+
def __init__(self, trio_socket: TrioSocketType) -> None:
|
| 538 |
+
super().__init__(trio_socket)
|
| 539 |
+
self._receive_guard = ResourceGuard("reading from")
|
| 540 |
+
self._send_guard = ResourceGuard("writing to")
|
| 541 |
+
|
| 542 |
+
async def receive(self) -> bytes:
|
| 543 |
+
with self._receive_guard:
|
| 544 |
+
try:
|
| 545 |
+
return await self._trio_socket.recv(65536)
|
| 546 |
+
except BaseException as exc:
|
| 547 |
+
self._convert_socket_error(exc)
|
| 548 |
+
|
| 549 |
+
async def send(self, item: bytes) -> None:
|
| 550 |
+
with self._send_guard:
|
| 551 |
+
try:
|
| 552 |
+
await self._trio_socket.send(item)
|
| 553 |
+
except BaseException as exc:
|
| 554 |
+
self._convert_socket_error(exc)
|
| 555 |
+
|
| 556 |
+
|
| 557 |
+
class UNIXDatagramSocket(_TrioSocketMixin[str], abc.UNIXDatagramSocket):
|
| 558 |
+
def __init__(self, trio_socket: TrioSocketType) -> None:
|
| 559 |
+
super().__init__(trio_socket)
|
| 560 |
+
self._receive_guard = ResourceGuard("reading from")
|
| 561 |
+
self._send_guard = ResourceGuard("writing to")
|
| 562 |
+
|
| 563 |
+
async def receive(self) -> UNIXDatagramPacketType:
|
| 564 |
+
with self._receive_guard:
|
| 565 |
+
try:
|
| 566 |
+
data, addr = await self._trio_socket.recvfrom(65536)
|
| 567 |
+
return data, addr
|
| 568 |
+
except BaseException as exc:
|
| 569 |
+
self._convert_socket_error(exc)
|
| 570 |
+
|
| 571 |
+
async def send(self, item: UNIXDatagramPacketType) -> None:
|
| 572 |
+
with self._send_guard:
|
| 573 |
+
try:
|
| 574 |
+
await self._trio_socket.sendto(*item)
|
| 575 |
+
except BaseException as exc:
|
| 576 |
+
self._convert_socket_error(exc)
|
| 577 |
+
|
| 578 |
+
|
| 579 |
+
class ConnectedUNIXDatagramSocket(
|
| 580 |
+
_TrioSocketMixin[str], abc.ConnectedUNIXDatagramSocket
|
| 581 |
+
):
|
| 582 |
+
def __init__(self, trio_socket: TrioSocketType) -> None:
|
| 583 |
+
super().__init__(trio_socket)
|
| 584 |
+
self._receive_guard = ResourceGuard("reading from")
|
| 585 |
+
self._send_guard = ResourceGuard("writing to")
|
| 586 |
+
|
| 587 |
+
async def receive(self) -> bytes:
|
| 588 |
+
with self._receive_guard:
|
| 589 |
+
try:
|
| 590 |
+
return await self._trio_socket.recv(65536)
|
| 591 |
+
except BaseException as exc:
|
| 592 |
+
self._convert_socket_error(exc)
|
| 593 |
+
|
| 594 |
+
async def send(self, item: bytes) -> None:
|
| 595 |
+
with self._send_guard:
|
| 596 |
+
try:
|
| 597 |
+
await self._trio_socket.send(item)
|
| 598 |
+
except BaseException as exc:
|
| 599 |
+
self._convert_socket_error(exc)
|
| 600 |
+
|
| 601 |
+
|
| 602 |
+
#
|
| 603 |
+
# Synchronization
|
| 604 |
+
#
|
| 605 |
+
|
| 606 |
+
|
| 607 |
+
class Event(BaseEvent):
|
| 608 |
+
def __new__(cls) -> Event:
|
| 609 |
+
return object.__new__(cls)
|
| 610 |
+
|
| 611 |
+
def __init__(self) -> None:
|
| 612 |
+
self.__original = trio.Event()
|
| 613 |
+
|
| 614 |
+
def is_set(self) -> bool:
|
| 615 |
+
return self.__original.is_set()
|
| 616 |
+
|
| 617 |
+
async def wait(self) -> None:
|
| 618 |
+
return await self.__original.wait()
|
| 619 |
+
|
| 620 |
+
def statistics(self) -> EventStatistics:
|
| 621 |
+
orig_statistics = self.__original.statistics()
|
| 622 |
+
return EventStatistics(tasks_waiting=orig_statistics.tasks_waiting)
|
| 623 |
+
|
| 624 |
+
def set(self) -> None:
|
| 625 |
+
self.__original.set()
|
| 626 |
+
|
| 627 |
+
|
| 628 |
+
class Lock(BaseLock):
|
| 629 |
+
def __new__(cls, *, fast_acquire: bool = False) -> Lock:
|
| 630 |
+
return object.__new__(cls)
|
| 631 |
+
|
| 632 |
+
def __init__(self, *, fast_acquire: bool = False) -> None:
|
| 633 |
+
self._fast_acquire = fast_acquire
|
| 634 |
+
self.__original = trio.Lock()
|
| 635 |
+
|
| 636 |
+
@staticmethod
|
| 637 |
+
def _convert_runtime_error_msg(exc: RuntimeError) -> None:
|
| 638 |
+
if exc.args == ("attempt to re-acquire an already held Lock",):
|
| 639 |
+
exc.args = ("Attempted to acquire an already held Lock",)
|
| 640 |
+
|
| 641 |
+
async def acquire(self) -> None:
|
| 642 |
+
if not self._fast_acquire:
|
| 643 |
+
try:
|
| 644 |
+
await self.__original.acquire()
|
| 645 |
+
except RuntimeError as exc:
|
| 646 |
+
self._convert_runtime_error_msg(exc)
|
| 647 |
+
raise
|
| 648 |
+
|
| 649 |
+
return
|
| 650 |
+
|
| 651 |
+
# This is the "fast path" where we don't let other tasks run
|
| 652 |
+
await trio.lowlevel.checkpoint_if_cancelled()
|
| 653 |
+
try:
|
| 654 |
+
self.__original.acquire_nowait()
|
| 655 |
+
except trio.WouldBlock:
|
| 656 |
+
await self.__original._lot.park()
|
| 657 |
+
except RuntimeError as exc:
|
| 658 |
+
self._convert_runtime_error_msg(exc)
|
| 659 |
+
raise
|
| 660 |
+
|
| 661 |
+
def acquire_nowait(self) -> None:
|
| 662 |
+
try:
|
| 663 |
+
self.__original.acquire_nowait()
|
| 664 |
+
except trio.WouldBlock:
|
| 665 |
+
raise WouldBlock from None
|
| 666 |
+
except RuntimeError as exc:
|
| 667 |
+
self._convert_runtime_error_msg(exc)
|
| 668 |
+
raise
|
| 669 |
+
|
| 670 |
+
def locked(self) -> bool:
|
| 671 |
+
return self.__original.locked()
|
| 672 |
+
|
| 673 |
+
def release(self) -> None:
|
| 674 |
+
self.__original.release()
|
| 675 |
+
|
| 676 |
+
def statistics(self) -> LockStatistics:
|
| 677 |
+
orig_statistics = self.__original.statistics()
|
| 678 |
+
owner = TrioTaskInfo(orig_statistics.owner) if orig_statistics.owner else None
|
| 679 |
+
return LockStatistics(
|
| 680 |
+
orig_statistics.locked, owner, orig_statistics.tasks_waiting
|
| 681 |
+
)
|
| 682 |
+
|
| 683 |
+
|
| 684 |
+
class Semaphore(BaseSemaphore):
|
| 685 |
+
def __new__(
|
| 686 |
+
cls,
|
| 687 |
+
initial_value: int,
|
| 688 |
+
*,
|
| 689 |
+
max_value: int | None = None,
|
| 690 |
+
fast_acquire: bool = False,
|
| 691 |
+
) -> Semaphore:
|
| 692 |
+
return object.__new__(cls)
|
| 693 |
+
|
| 694 |
+
def __init__(
|
| 695 |
+
self,
|
| 696 |
+
initial_value: int,
|
| 697 |
+
*,
|
| 698 |
+
max_value: int | None = None,
|
| 699 |
+
fast_acquire: bool = False,
|
| 700 |
+
) -> None:
|
| 701 |
+
super().__init__(initial_value, max_value=max_value, fast_acquire=fast_acquire)
|
| 702 |
+
self.__original = trio.Semaphore(initial_value, max_value=max_value)
|
| 703 |
+
|
| 704 |
+
async def acquire(self) -> None:
|
| 705 |
+
if not self._fast_acquire:
|
| 706 |
+
await self.__original.acquire()
|
| 707 |
+
return
|
| 708 |
+
|
| 709 |
+
# This is the "fast path" where we don't let other tasks run
|
| 710 |
+
await trio.lowlevel.checkpoint_if_cancelled()
|
| 711 |
+
try:
|
| 712 |
+
self.__original.acquire_nowait()
|
| 713 |
+
except trio.WouldBlock:
|
| 714 |
+
await self.__original._lot.park()
|
| 715 |
+
|
| 716 |
+
def acquire_nowait(self) -> None:
|
| 717 |
+
try:
|
| 718 |
+
self.__original.acquire_nowait()
|
| 719 |
+
except trio.WouldBlock:
|
| 720 |
+
raise WouldBlock from None
|
| 721 |
+
|
| 722 |
+
@property
|
| 723 |
+
def max_value(self) -> int | None:
|
| 724 |
+
return self.__original.max_value
|
| 725 |
+
|
| 726 |
+
@property
|
| 727 |
+
def value(self) -> int:
|
| 728 |
+
return self.__original.value
|
| 729 |
+
|
| 730 |
+
def release(self) -> None:
|
| 731 |
+
self.__original.release()
|
| 732 |
+
|
| 733 |
+
def statistics(self) -> SemaphoreStatistics:
|
| 734 |
+
orig_statistics = self.__original.statistics()
|
| 735 |
+
return SemaphoreStatistics(orig_statistics.tasks_waiting)
|
| 736 |
+
|
| 737 |
+
|
| 738 |
+
class CapacityLimiter(BaseCapacityLimiter):
|
| 739 |
+
def __new__(
|
| 740 |
+
cls,
|
| 741 |
+
total_tokens: float | None = None,
|
| 742 |
+
*,
|
| 743 |
+
original: trio.CapacityLimiter | None = None,
|
| 744 |
+
) -> CapacityLimiter:
|
| 745 |
+
return object.__new__(cls)
|
| 746 |
+
|
| 747 |
+
def __init__(
|
| 748 |
+
self,
|
| 749 |
+
total_tokens: float | None = None,
|
| 750 |
+
*,
|
| 751 |
+
original: trio.CapacityLimiter | None = None,
|
| 752 |
+
) -> None:
|
| 753 |
+
if original is not None:
|
| 754 |
+
self.__original = original
|
| 755 |
+
else:
|
| 756 |
+
assert total_tokens is not None
|
| 757 |
+
self.__original = trio.CapacityLimiter(total_tokens)
|
| 758 |
+
|
| 759 |
+
async def __aenter__(self) -> None:
|
| 760 |
+
return await self.__original.__aenter__()
|
| 761 |
+
|
| 762 |
+
async def __aexit__(
|
| 763 |
+
self,
|
| 764 |
+
exc_type: type[BaseException] | None,
|
| 765 |
+
exc_val: BaseException | None,
|
| 766 |
+
exc_tb: TracebackType | None,
|
| 767 |
+
) -> None:
|
| 768 |
+
await self.__original.__aexit__(exc_type, exc_val, exc_tb)
|
| 769 |
+
|
| 770 |
+
@property
|
| 771 |
+
def total_tokens(self) -> float:
|
| 772 |
+
return self.__original.total_tokens
|
| 773 |
+
|
| 774 |
+
@total_tokens.setter
|
| 775 |
+
def total_tokens(self, value: float) -> None:
|
| 776 |
+
self.__original.total_tokens = value
|
| 777 |
+
|
| 778 |
+
@property
|
| 779 |
+
def borrowed_tokens(self) -> int:
|
| 780 |
+
return self.__original.borrowed_tokens
|
| 781 |
+
|
| 782 |
+
@property
|
| 783 |
+
def available_tokens(self) -> float:
|
| 784 |
+
return self.__original.available_tokens
|
| 785 |
+
|
| 786 |
+
def acquire_nowait(self) -> None:
|
| 787 |
+
self.__original.acquire_nowait()
|
| 788 |
+
|
| 789 |
+
def acquire_on_behalf_of_nowait(self, borrower: object) -> None:
|
| 790 |
+
self.__original.acquire_on_behalf_of_nowait(borrower)
|
| 791 |
+
|
| 792 |
+
async def acquire(self) -> None:
|
| 793 |
+
await self.__original.acquire()
|
| 794 |
+
|
| 795 |
+
async def acquire_on_behalf_of(self, borrower: object) -> None:
|
| 796 |
+
await self.__original.acquire_on_behalf_of(borrower)
|
| 797 |
+
|
| 798 |
+
def release(self) -> None:
|
| 799 |
+
return self.__original.release()
|
| 800 |
+
|
| 801 |
+
def release_on_behalf_of(self, borrower: object) -> None:
|
| 802 |
+
return self.__original.release_on_behalf_of(borrower)
|
| 803 |
+
|
| 804 |
+
def statistics(self) -> CapacityLimiterStatistics:
|
| 805 |
+
orig = self.__original.statistics()
|
| 806 |
+
return CapacityLimiterStatistics(
|
| 807 |
+
borrowed_tokens=orig.borrowed_tokens,
|
| 808 |
+
total_tokens=orig.total_tokens,
|
| 809 |
+
borrowers=tuple(orig.borrowers),
|
| 810 |
+
tasks_waiting=orig.tasks_waiting,
|
| 811 |
+
)
|
| 812 |
+
|
| 813 |
+
|
| 814 |
+
_capacity_limiter_wrapper: trio.lowlevel.RunVar = RunVar("_capacity_limiter_wrapper")
|
| 815 |
+
|
| 816 |
+
|
| 817 |
+
#
|
| 818 |
+
# Signal handling
|
| 819 |
+
#
|
| 820 |
+
|
| 821 |
+
|
| 822 |
+
class _SignalReceiver:
|
| 823 |
+
_iterator: AsyncIterator[int]
|
| 824 |
+
|
| 825 |
+
def __init__(self, signals: tuple[Signals, ...]):
|
| 826 |
+
self._signals = signals
|
| 827 |
+
|
| 828 |
+
def __enter__(self) -> _SignalReceiver:
|
| 829 |
+
self._cm = trio.open_signal_receiver(*self._signals)
|
| 830 |
+
self._iterator = self._cm.__enter__()
|
| 831 |
+
return self
|
| 832 |
+
|
| 833 |
+
def __exit__(
|
| 834 |
+
self,
|
| 835 |
+
exc_type: type[BaseException] | None,
|
| 836 |
+
exc_val: BaseException | None,
|
| 837 |
+
exc_tb: TracebackType | None,
|
| 838 |
+
) -> bool | None:
|
| 839 |
+
return self._cm.__exit__(exc_type, exc_val, exc_tb)
|
| 840 |
+
|
| 841 |
+
def __aiter__(self) -> _SignalReceiver:
|
| 842 |
+
return self
|
| 843 |
+
|
| 844 |
+
async def __anext__(self) -> Signals:
|
| 845 |
+
signum = await self._iterator.__anext__()
|
| 846 |
+
return Signals(signum)
|
| 847 |
+
|
| 848 |
+
|
| 849 |
+
#
|
| 850 |
+
# Testing and debugging
|
| 851 |
+
#
|
| 852 |
+
|
| 853 |
+
|
| 854 |
+
class TestRunner(abc.TestRunner):
|
| 855 |
+
def __init__(self, **options: Any) -> None:
|
| 856 |
+
from queue import Queue
|
| 857 |
+
|
| 858 |
+
self._call_queue: Queue[Callable[[], object]] = Queue()
|
| 859 |
+
self._send_stream: MemoryObjectSendStream | None = None
|
| 860 |
+
self._options = options
|
| 861 |
+
|
| 862 |
+
def __exit__(
|
| 863 |
+
self,
|
| 864 |
+
exc_type: type[BaseException] | None,
|
| 865 |
+
exc_val: BaseException | None,
|
| 866 |
+
exc_tb: types.TracebackType | None,
|
| 867 |
+
) -> None:
|
| 868 |
+
if self._send_stream:
|
| 869 |
+
self._send_stream.close()
|
| 870 |
+
while self._send_stream is not None:
|
| 871 |
+
self._call_queue.get()()
|
| 872 |
+
|
| 873 |
+
async def _run_tests_and_fixtures(self) -> None:
|
| 874 |
+
self._send_stream, receive_stream = create_memory_object_stream(1)
|
| 875 |
+
with receive_stream:
|
| 876 |
+
async for coro, outcome_holder in receive_stream:
|
| 877 |
+
try:
|
| 878 |
+
retval = await coro
|
| 879 |
+
except BaseException as exc:
|
| 880 |
+
outcome_holder.append(Error(exc))
|
| 881 |
+
else:
|
| 882 |
+
outcome_holder.append(Value(retval))
|
| 883 |
+
|
| 884 |
+
def _main_task_finished(self, outcome: object) -> None:
|
| 885 |
+
self._send_stream = None
|
| 886 |
+
|
| 887 |
+
def _call_in_runner_task(
|
| 888 |
+
self,
|
| 889 |
+
func: Callable[P, Awaitable[T_Retval]],
|
| 890 |
+
*args: P.args,
|
| 891 |
+
**kwargs: P.kwargs,
|
| 892 |
+
) -> T_Retval:
|
| 893 |
+
if self._send_stream is None:
|
| 894 |
+
trio.lowlevel.start_guest_run(
|
| 895 |
+
self._run_tests_and_fixtures,
|
| 896 |
+
run_sync_soon_threadsafe=self._call_queue.put,
|
| 897 |
+
done_callback=self._main_task_finished,
|
| 898 |
+
**self._options,
|
| 899 |
+
)
|
| 900 |
+
while self._send_stream is None:
|
| 901 |
+
self._call_queue.get()()
|
| 902 |
+
|
| 903 |
+
outcome_holder: list[Outcome] = []
|
| 904 |
+
self._send_stream.send_nowait((func(*args, **kwargs), outcome_holder))
|
| 905 |
+
while not outcome_holder:
|
| 906 |
+
self._call_queue.get()()
|
| 907 |
+
|
| 908 |
+
return outcome_holder[0].unwrap()
|
| 909 |
+
|
| 910 |
+
def run_asyncgen_fixture(
|
| 911 |
+
self,
|
| 912 |
+
fixture_func: Callable[..., AsyncGenerator[T_Retval, Any]],
|
| 913 |
+
kwargs: dict[str, Any],
|
| 914 |
+
) -> Iterable[T_Retval]:
|
| 915 |
+
asyncgen = fixture_func(**kwargs)
|
| 916 |
+
fixturevalue: T_Retval = self._call_in_runner_task(asyncgen.asend, None)
|
| 917 |
+
|
| 918 |
+
yield fixturevalue
|
| 919 |
+
|
| 920 |
+
try:
|
| 921 |
+
self._call_in_runner_task(asyncgen.asend, None)
|
| 922 |
+
except StopAsyncIteration:
|
| 923 |
+
pass
|
| 924 |
+
else:
|
| 925 |
+
self._call_in_runner_task(asyncgen.aclose)
|
| 926 |
+
raise RuntimeError("Async generator fixture did not stop")
|
| 927 |
+
|
| 928 |
+
def run_fixture(
|
| 929 |
+
self,
|
| 930 |
+
fixture_func: Callable[..., Coroutine[Any, Any, T_Retval]],
|
| 931 |
+
kwargs: dict[str, Any],
|
| 932 |
+
) -> T_Retval:
|
| 933 |
+
return self._call_in_runner_task(fixture_func, **kwargs)
|
| 934 |
+
|
| 935 |
+
def run_test(
|
| 936 |
+
self, test_func: Callable[..., Coroutine[Any, Any, Any]], kwargs: dict[str, Any]
|
| 937 |
+
) -> None:
|
| 938 |
+
self._call_in_runner_task(test_func, **kwargs)
|
| 939 |
+
|
| 940 |
+
|
| 941 |
+
class TrioTaskInfo(TaskInfo):
|
| 942 |
+
def __init__(self, task: trio.lowlevel.Task):
|
| 943 |
+
parent_id = None
|
| 944 |
+
if task.parent_nursery and task.parent_nursery.parent_task:
|
| 945 |
+
parent_id = id(task.parent_nursery.parent_task)
|
| 946 |
+
|
| 947 |
+
super().__init__(id(task), parent_id, task.name, task.coro)
|
| 948 |
+
self._task = weakref.proxy(task)
|
| 949 |
+
|
| 950 |
+
def has_pending_cancellation(self) -> bool:
|
| 951 |
+
try:
|
| 952 |
+
return self._task._cancel_status.effectively_cancelled
|
| 953 |
+
except ReferenceError:
|
| 954 |
+
# If the task is no longer around, it surely doesn't have a cancellation
|
| 955 |
+
# pending
|
| 956 |
+
return False
|
| 957 |
+
|
| 958 |
+
|
| 959 |
+
class TrioBackend(AsyncBackend):
|
| 960 |
+
@classmethod
|
| 961 |
+
def run(
|
| 962 |
+
cls,
|
| 963 |
+
func: Callable[[Unpack[PosArgsT]], Awaitable[T_Retval]],
|
| 964 |
+
args: tuple[Unpack[PosArgsT]],
|
| 965 |
+
kwargs: dict[str, Any],
|
| 966 |
+
options: dict[str, Any],
|
| 967 |
+
) -> T_Retval:
|
| 968 |
+
return trio.run(func, *args)
|
| 969 |
+
|
| 970 |
+
@classmethod
|
| 971 |
+
def current_token(cls) -> object:
|
| 972 |
+
return trio.lowlevel.current_trio_token()
|
| 973 |
+
|
| 974 |
+
@classmethod
|
| 975 |
+
def current_time(cls) -> float:
|
| 976 |
+
return trio.current_time()
|
| 977 |
+
|
| 978 |
+
@classmethod
|
| 979 |
+
def cancelled_exception_class(cls) -> type[BaseException]:
|
| 980 |
+
return trio.Cancelled
|
| 981 |
+
|
| 982 |
+
@classmethod
|
| 983 |
+
async def checkpoint(cls) -> None:
|
| 984 |
+
await trio.lowlevel.checkpoint()
|
| 985 |
+
|
| 986 |
+
@classmethod
|
| 987 |
+
async def checkpoint_if_cancelled(cls) -> None:
|
| 988 |
+
await trio.lowlevel.checkpoint_if_cancelled()
|
| 989 |
+
|
| 990 |
+
@classmethod
|
| 991 |
+
async def cancel_shielded_checkpoint(cls) -> None:
|
| 992 |
+
await trio.lowlevel.cancel_shielded_checkpoint()
|
| 993 |
+
|
| 994 |
+
@classmethod
|
| 995 |
+
async def sleep(cls, delay: float) -> None:
|
| 996 |
+
await trio.sleep(delay)
|
| 997 |
+
|
| 998 |
+
@classmethod
|
| 999 |
+
def create_cancel_scope(
|
| 1000 |
+
cls, *, deadline: float = math.inf, shield: bool = False
|
| 1001 |
+
) -> abc.CancelScope:
|
| 1002 |
+
return CancelScope(deadline=deadline, shield=shield)
|
| 1003 |
+
|
| 1004 |
+
@classmethod
|
| 1005 |
+
def current_effective_deadline(cls) -> float:
|
| 1006 |
+
return trio.current_effective_deadline()
|
| 1007 |
+
|
| 1008 |
+
@classmethod
|
| 1009 |
+
def create_task_group(cls) -> abc.TaskGroup:
|
| 1010 |
+
return TaskGroup()
|
| 1011 |
+
|
| 1012 |
+
@classmethod
|
| 1013 |
+
def create_event(cls) -> abc.Event:
|
| 1014 |
+
return Event()
|
| 1015 |
+
|
| 1016 |
+
@classmethod
|
| 1017 |
+
def create_lock(cls, *, fast_acquire: bool) -> Lock:
|
| 1018 |
+
return Lock(fast_acquire=fast_acquire)
|
| 1019 |
+
|
| 1020 |
+
@classmethod
|
| 1021 |
+
def create_semaphore(
|
| 1022 |
+
cls,
|
| 1023 |
+
initial_value: int,
|
| 1024 |
+
*,
|
| 1025 |
+
max_value: int | None = None,
|
| 1026 |
+
fast_acquire: bool = False,
|
| 1027 |
+
) -> abc.Semaphore:
|
| 1028 |
+
return Semaphore(initial_value, max_value=max_value, fast_acquire=fast_acquire)
|
| 1029 |
+
|
| 1030 |
+
@classmethod
|
| 1031 |
+
def create_capacity_limiter(cls, total_tokens: float) -> CapacityLimiter:
|
| 1032 |
+
return CapacityLimiter(total_tokens)
|
| 1033 |
+
|
| 1034 |
+
@classmethod
|
| 1035 |
+
async def run_sync_in_worker_thread(
|
| 1036 |
+
cls,
|
| 1037 |
+
func: Callable[[Unpack[PosArgsT]], T_Retval],
|
| 1038 |
+
args: tuple[Unpack[PosArgsT]],
|
| 1039 |
+
abandon_on_cancel: bool = False,
|
| 1040 |
+
limiter: abc.CapacityLimiter | None = None,
|
| 1041 |
+
) -> T_Retval:
|
| 1042 |
+
def wrapper() -> T_Retval:
|
| 1043 |
+
with claim_worker_thread(TrioBackend, token):
|
| 1044 |
+
return func(*args)
|
| 1045 |
+
|
| 1046 |
+
token = TrioBackend.current_token()
|
| 1047 |
+
return await run_sync(
|
| 1048 |
+
wrapper,
|
| 1049 |
+
abandon_on_cancel=abandon_on_cancel,
|
| 1050 |
+
limiter=cast(trio.CapacityLimiter, limiter),
|
| 1051 |
+
)
|
| 1052 |
+
|
| 1053 |
+
@classmethod
|
| 1054 |
+
def check_cancelled(cls) -> None:
|
| 1055 |
+
trio.from_thread.check_cancelled()
|
| 1056 |
+
|
| 1057 |
+
@classmethod
|
| 1058 |
+
def run_async_from_thread(
|
| 1059 |
+
cls,
|
| 1060 |
+
func: Callable[[Unpack[PosArgsT]], Awaitable[T_Retval]],
|
| 1061 |
+
args: tuple[Unpack[PosArgsT]],
|
| 1062 |
+
token: object,
|
| 1063 |
+
) -> T_Retval:
|
| 1064 |
+
trio_token = cast("trio.lowlevel.TrioToken | None", token)
|
| 1065 |
+
try:
|
| 1066 |
+
return trio.from_thread.run(func, *args, trio_token=trio_token)
|
| 1067 |
+
except trio.RunFinishedError:
|
| 1068 |
+
raise RunFinishedError from None
|
| 1069 |
+
|
| 1070 |
+
@classmethod
|
| 1071 |
+
def run_sync_from_thread(
|
| 1072 |
+
cls,
|
| 1073 |
+
func: Callable[[Unpack[PosArgsT]], T_Retval],
|
| 1074 |
+
args: tuple[Unpack[PosArgsT]],
|
| 1075 |
+
token: object,
|
| 1076 |
+
) -> T_Retval:
|
| 1077 |
+
trio_token = cast("trio.lowlevel.TrioToken | None", token)
|
| 1078 |
+
try:
|
| 1079 |
+
return trio.from_thread.run_sync(func, *args, trio_token=trio_token)
|
| 1080 |
+
except trio.RunFinishedError:
|
| 1081 |
+
raise RunFinishedError from None
|
| 1082 |
+
|
| 1083 |
+
@classmethod
|
| 1084 |
+
async def open_process(
|
| 1085 |
+
cls,
|
| 1086 |
+
command: StrOrBytesPath | Sequence[StrOrBytesPath],
|
| 1087 |
+
*,
|
| 1088 |
+
stdin: int | IO[Any] | None,
|
| 1089 |
+
stdout: int | IO[Any] | None,
|
| 1090 |
+
stderr: int | IO[Any] | None,
|
| 1091 |
+
**kwargs: Any,
|
| 1092 |
+
) -> Process:
|
| 1093 |
+
def convert_item(item: StrOrBytesPath) -> str:
|
| 1094 |
+
str_or_bytes = os.fspath(item)
|
| 1095 |
+
if isinstance(str_or_bytes, str):
|
| 1096 |
+
return str_or_bytes
|
| 1097 |
+
else:
|
| 1098 |
+
return os.fsdecode(str_or_bytes)
|
| 1099 |
+
|
| 1100 |
+
if isinstance(command, (str, bytes, PathLike)):
|
| 1101 |
+
process = await trio.lowlevel.open_process(
|
| 1102 |
+
convert_item(command),
|
| 1103 |
+
stdin=stdin,
|
| 1104 |
+
stdout=stdout,
|
| 1105 |
+
stderr=stderr,
|
| 1106 |
+
shell=True,
|
| 1107 |
+
**kwargs,
|
| 1108 |
+
)
|
| 1109 |
+
else:
|
| 1110 |
+
process = await trio.lowlevel.open_process(
|
| 1111 |
+
[convert_item(item) for item in command],
|
| 1112 |
+
stdin=stdin,
|
| 1113 |
+
stdout=stdout,
|
| 1114 |
+
stderr=stderr,
|
| 1115 |
+
shell=False,
|
| 1116 |
+
**kwargs,
|
| 1117 |
+
)
|
| 1118 |
+
|
| 1119 |
+
stdin_stream = SendStreamWrapper(process.stdin) if process.stdin else None
|
| 1120 |
+
stdout_stream = ReceiveStreamWrapper(process.stdout) if process.stdout else None
|
| 1121 |
+
stderr_stream = ReceiveStreamWrapper(process.stderr) if process.stderr else None
|
| 1122 |
+
return Process(process, stdin_stream, stdout_stream, stderr_stream)
|
| 1123 |
+
|
| 1124 |
+
@classmethod
|
| 1125 |
+
def setup_process_pool_exit_at_shutdown(cls, workers: set[abc.Process]) -> None:
|
| 1126 |
+
trio.lowlevel.spawn_system_task(_shutdown_process_pool, workers)
|
| 1127 |
+
|
| 1128 |
+
@classmethod
|
| 1129 |
+
async def connect_tcp(
|
| 1130 |
+
cls, host: str, port: int, local_address: IPSockAddrType | None = None
|
| 1131 |
+
) -> SocketStream:
|
| 1132 |
+
family = socket.AF_INET6 if ":" in host else socket.AF_INET
|
| 1133 |
+
trio_socket = trio.socket.socket(family)
|
| 1134 |
+
trio_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
| 1135 |
+
if local_address:
|
| 1136 |
+
await trio_socket.bind(local_address)
|
| 1137 |
+
|
| 1138 |
+
try:
|
| 1139 |
+
await trio_socket.connect((host, port))
|
| 1140 |
+
except BaseException:
|
| 1141 |
+
trio_socket.close()
|
| 1142 |
+
raise
|
| 1143 |
+
|
| 1144 |
+
return SocketStream(trio_socket)
|
| 1145 |
+
|
| 1146 |
+
@classmethod
|
| 1147 |
+
async def connect_unix(cls, path: str | bytes) -> abc.UNIXSocketStream:
|
| 1148 |
+
trio_socket = trio.socket.socket(socket.AF_UNIX)
|
| 1149 |
+
try:
|
| 1150 |
+
await trio_socket.connect(path)
|
| 1151 |
+
except BaseException:
|
| 1152 |
+
trio_socket.close()
|
| 1153 |
+
raise
|
| 1154 |
+
|
| 1155 |
+
return UNIXSocketStream(trio_socket)
|
| 1156 |
+
|
| 1157 |
+
@classmethod
|
| 1158 |
+
def create_tcp_listener(cls, sock: socket.socket) -> abc.SocketListener:
|
| 1159 |
+
return TCPSocketListener(sock)
|
| 1160 |
+
|
| 1161 |
+
@classmethod
|
| 1162 |
+
def create_unix_listener(cls, sock: socket.socket) -> abc.SocketListener:
|
| 1163 |
+
return UNIXSocketListener(sock)
|
| 1164 |
+
|
| 1165 |
+
@classmethod
|
| 1166 |
+
async def create_udp_socket(
|
| 1167 |
+
cls,
|
| 1168 |
+
family: socket.AddressFamily,
|
| 1169 |
+
local_address: IPSockAddrType | None,
|
| 1170 |
+
remote_address: IPSockAddrType | None,
|
| 1171 |
+
reuse_port: bool,
|
| 1172 |
+
) -> UDPSocket | ConnectedUDPSocket:
|
| 1173 |
+
trio_socket = trio.socket.socket(family=family, type=socket.SOCK_DGRAM)
|
| 1174 |
+
|
| 1175 |
+
if reuse_port:
|
| 1176 |
+
trio_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
| 1177 |
+
|
| 1178 |
+
if local_address:
|
| 1179 |
+
await trio_socket.bind(local_address)
|
| 1180 |
+
|
| 1181 |
+
if remote_address:
|
| 1182 |
+
await trio_socket.connect(remote_address)
|
| 1183 |
+
return ConnectedUDPSocket(trio_socket)
|
| 1184 |
+
else:
|
| 1185 |
+
return UDPSocket(trio_socket)
|
| 1186 |
+
|
| 1187 |
+
@classmethod
|
| 1188 |
+
@overload
|
| 1189 |
+
async def create_unix_datagram_socket(
|
| 1190 |
+
cls, raw_socket: socket.socket, remote_path: None
|
| 1191 |
+
) -> abc.UNIXDatagramSocket: ...
|
| 1192 |
+
|
| 1193 |
+
@classmethod
|
| 1194 |
+
@overload
|
| 1195 |
+
async def create_unix_datagram_socket(
|
| 1196 |
+
cls, raw_socket: socket.socket, remote_path: str | bytes
|
| 1197 |
+
) -> abc.ConnectedUNIXDatagramSocket: ...
|
| 1198 |
+
|
| 1199 |
+
@classmethod
|
| 1200 |
+
async def create_unix_datagram_socket(
|
| 1201 |
+
cls, raw_socket: socket.socket, remote_path: str | bytes | None
|
| 1202 |
+
) -> abc.UNIXDatagramSocket | abc.ConnectedUNIXDatagramSocket:
|
| 1203 |
+
trio_socket = trio.socket.from_stdlib_socket(raw_socket)
|
| 1204 |
+
|
| 1205 |
+
if remote_path:
|
| 1206 |
+
await trio_socket.connect(remote_path)
|
| 1207 |
+
return ConnectedUNIXDatagramSocket(trio_socket)
|
| 1208 |
+
else:
|
| 1209 |
+
return UNIXDatagramSocket(trio_socket)
|
| 1210 |
+
|
| 1211 |
+
@classmethod
|
| 1212 |
+
async def getaddrinfo(
|
| 1213 |
+
cls,
|
| 1214 |
+
host: bytes | str | None,
|
| 1215 |
+
port: str | int | None,
|
| 1216 |
+
*,
|
| 1217 |
+
family: int | AddressFamily = 0,
|
| 1218 |
+
type: int | SocketKind = 0,
|
| 1219 |
+
proto: int = 0,
|
| 1220 |
+
flags: int = 0,
|
| 1221 |
+
) -> Sequence[
|
| 1222 |
+
tuple[
|
| 1223 |
+
AddressFamily,
|
| 1224 |
+
SocketKind,
|
| 1225 |
+
int,
|
| 1226 |
+
str,
|
| 1227 |
+
tuple[str, int] | tuple[str, int, int, int] | tuple[int, bytes],
|
| 1228 |
+
]
|
| 1229 |
+
]:
|
| 1230 |
+
return await trio.socket.getaddrinfo(host, port, family, type, proto, flags)
|
| 1231 |
+
|
| 1232 |
+
@classmethod
|
| 1233 |
+
async def getnameinfo(
|
| 1234 |
+
cls, sockaddr: IPSockAddrType, flags: int = 0
|
| 1235 |
+
) -> tuple[str, str]:
|
| 1236 |
+
return await trio.socket.getnameinfo(sockaddr, flags)
|
| 1237 |
+
|
| 1238 |
+
@classmethod
|
| 1239 |
+
async def wait_readable(cls, obj: FileDescriptorLike) -> None:
|
| 1240 |
+
try:
|
| 1241 |
+
await wait_readable(obj)
|
| 1242 |
+
except trio.ClosedResourceError as exc:
|
| 1243 |
+
raise ClosedResourceError().with_traceback(exc.__traceback__) from None
|
| 1244 |
+
except trio.BusyResourceError:
|
| 1245 |
+
raise BusyResourceError("reading from") from None
|
| 1246 |
+
|
| 1247 |
+
@classmethod
|
| 1248 |
+
async def wait_writable(cls, obj: FileDescriptorLike) -> None:
|
| 1249 |
+
try:
|
| 1250 |
+
await wait_writable(obj)
|
| 1251 |
+
except trio.ClosedResourceError as exc:
|
| 1252 |
+
raise ClosedResourceError().with_traceback(exc.__traceback__) from None
|
| 1253 |
+
except trio.BusyResourceError:
|
| 1254 |
+
raise BusyResourceError("writing to") from None
|
| 1255 |
+
|
| 1256 |
+
@classmethod
|
| 1257 |
+
def notify_closing(cls, obj: FileDescriptorLike) -> None:
|
| 1258 |
+
notify_closing(obj)
|
| 1259 |
+
|
| 1260 |
+
@classmethod
|
| 1261 |
+
async def wrap_listener_socket(cls, sock: socket.socket) -> abc.SocketListener:
|
| 1262 |
+
return TCPSocketListener(sock)
|
| 1263 |
+
|
| 1264 |
+
@classmethod
|
| 1265 |
+
async def wrap_stream_socket(cls, sock: socket.socket) -> SocketStream:
|
| 1266 |
+
trio_sock = trio.socket.from_stdlib_socket(sock)
|
| 1267 |
+
return SocketStream(trio_sock)
|
| 1268 |
+
|
| 1269 |
+
@classmethod
|
| 1270 |
+
async def wrap_unix_stream_socket(cls, sock: socket.socket) -> UNIXSocketStream:
|
| 1271 |
+
trio_sock = trio.socket.from_stdlib_socket(sock)
|
| 1272 |
+
return UNIXSocketStream(trio_sock)
|
| 1273 |
+
|
| 1274 |
+
@classmethod
|
| 1275 |
+
async def wrap_udp_socket(cls, sock: socket.socket) -> UDPSocket:
|
| 1276 |
+
trio_sock = trio.socket.from_stdlib_socket(sock)
|
| 1277 |
+
return UDPSocket(trio_sock)
|
| 1278 |
+
|
| 1279 |
+
@classmethod
|
| 1280 |
+
async def wrap_connected_udp_socket(cls, sock: socket.socket) -> ConnectedUDPSocket:
|
| 1281 |
+
trio_sock = trio.socket.from_stdlib_socket(sock)
|
| 1282 |
+
return ConnectedUDPSocket(trio_sock)
|
| 1283 |
+
|
| 1284 |
+
@classmethod
|
| 1285 |
+
async def wrap_unix_datagram_socket(cls, sock: socket.socket) -> UNIXDatagramSocket:
|
| 1286 |
+
trio_sock = trio.socket.from_stdlib_socket(sock)
|
| 1287 |
+
return UNIXDatagramSocket(trio_sock)
|
| 1288 |
+
|
| 1289 |
+
@classmethod
|
| 1290 |
+
async def wrap_connected_unix_datagram_socket(
|
| 1291 |
+
cls, sock: socket.socket
|
| 1292 |
+
) -> ConnectedUNIXDatagramSocket:
|
| 1293 |
+
trio_sock = trio.socket.from_stdlib_socket(sock)
|
| 1294 |
+
return ConnectedUNIXDatagramSocket(trio_sock)
|
| 1295 |
+
|
| 1296 |
+
@classmethod
|
| 1297 |
+
def current_default_thread_limiter(cls) -> CapacityLimiter:
|
| 1298 |
+
try:
|
| 1299 |
+
return _capacity_limiter_wrapper.get()
|
| 1300 |
+
except LookupError:
|
| 1301 |
+
limiter = CapacityLimiter(
|
| 1302 |
+
original=trio.to_thread.current_default_thread_limiter()
|
| 1303 |
+
)
|
| 1304 |
+
_capacity_limiter_wrapper.set(limiter)
|
| 1305 |
+
return limiter
|
| 1306 |
+
|
| 1307 |
+
@classmethod
|
| 1308 |
+
def open_signal_receiver(
|
| 1309 |
+
cls, *signals: Signals
|
| 1310 |
+
) -> AbstractContextManager[AsyncIterator[Signals]]:
|
| 1311 |
+
return _SignalReceiver(signals)
|
| 1312 |
+
|
| 1313 |
+
@classmethod
|
| 1314 |
+
def get_current_task(cls) -> TaskInfo:
|
| 1315 |
+
task = current_task()
|
| 1316 |
+
return TrioTaskInfo(task)
|
| 1317 |
+
|
| 1318 |
+
@classmethod
|
| 1319 |
+
def get_running_tasks(cls) -> Sequence[TaskInfo]:
|
| 1320 |
+
root_task = current_root_task()
|
| 1321 |
+
assert root_task
|
| 1322 |
+
task_infos = [TrioTaskInfo(root_task)]
|
| 1323 |
+
nurseries = root_task.child_nurseries
|
| 1324 |
+
while nurseries:
|
| 1325 |
+
new_nurseries: list[trio.Nursery] = []
|
| 1326 |
+
for nursery in nurseries:
|
| 1327 |
+
for task in nursery.child_tasks:
|
| 1328 |
+
task_infos.append(TrioTaskInfo(task))
|
| 1329 |
+
new_nurseries.extend(task.child_nurseries)
|
| 1330 |
+
|
| 1331 |
+
nurseries = new_nurseries
|
| 1332 |
+
|
| 1333 |
+
return task_infos
|
| 1334 |
+
|
| 1335 |
+
@classmethod
|
| 1336 |
+
async def wait_all_tasks_blocked(cls) -> None:
|
| 1337 |
+
from trio.testing import wait_all_tasks_blocked
|
| 1338 |
+
|
| 1339 |
+
await wait_all_tasks_blocked()
|
| 1340 |
+
|
| 1341 |
+
@classmethod
|
| 1342 |
+
def create_test_runner(cls, options: dict[str, Any]) -> TestRunner:
|
| 1343 |
+
return TestRunner(**options)
|
| 1344 |
+
|
| 1345 |
+
|
| 1346 |
+
backend_class = TrioBackend
|
env/lib/python3.13/site-packages/anyio/_core/__init__.py
ADDED
|
File without changes
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (193 Bytes). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_asyncio_selector_thread.cpython-313.pyc
ADDED
|
Binary file (8.64 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_contextmanagers.cpython-313.pyc
ADDED
|
Binary file (9.07 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_eventloop.cpython-313.pyc
ADDED
|
Binary file (8.12 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_exceptions.cpython-313.pyc
ADDED
|
Binary file (7.66 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_fileio.cpython-313.pyc
ADDED
|
Binary file (43.3 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_resources.cpython-313.pyc
ADDED
|
Binary file (929 Bytes). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_signals.cpython-313.pyc
ADDED
|
Binary file (1.34 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_sockets.cpython-313.pyc
ADDED
|
Binary file (40.1 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_streams.cpython-313.pyc
ADDED
|
Binary file (2.38 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_subprocesses.cpython-313.pyc
ADDED
|
Binary file (9.45 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_synchronization.cpython-313.pyc
ADDED
|
Binary file (33 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_tasks.cpython-313.pyc
ADDED
|
Binary file (7.51 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_tempfile.cpython-313.pyc
ADDED
|
Binary file (27.9 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_testing.cpython-313.pyc
ADDED
|
Binary file (3.81 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/__pycache__/_typedattr.cpython-313.pyc
ADDED
|
Binary file (3.78 kB). View file
|
|
|
env/lib/python3.13/site-packages/anyio/_core/_asyncio_selector_thread.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import asyncio
|
| 4 |
+
import socket
|
| 5 |
+
import threading
|
| 6 |
+
from collections.abc import Callable
|
| 7 |
+
from selectors import EVENT_READ, EVENT_WRITE, DefaultSelector
|
| 8 |
+
from typing import TYPE_CHECKING, Any
|
| 9 |
+
|
| 10 |
+
if TYPE_CHECKING:
|
| 11 |
+
from _typeshed import FileDescriptorLike
|
| 12 |
+
|
| 13 |
+
_selector_lock = threading.Lock()
|
| 14 |
+
_selector: Selector | None = None
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class Selector:
|
| 18 |
+
def __init__(self) -> None:
|
| 19 |
+
self._thread = threading.Thread(target=self.run, name="AnyIO socket selector")
|
| 20 |
+
self._selector = DefaultSelector()
|
| 21 |
+
self._send, self._receive = socket.socketpair()
|
| 22 |
+
self._send.setblocking(False)
|
| 23 |
+
self._receive.setblocking(False)
|
| 24 |
+
# This somewhat reduces the amount of memory wasted queueing up data
|
| 25 |
+
# for wakeups. With these settings, maximum number of 1-byte sends
|
| 26 |
+
# before getting BlockingIOError:
|
| 27 |
+
# Linux 4.8: 6
|
| 28 |
+
# macOS (darwin 15.5): 1
|
| 29 |
+
# Windows 10: 525347
|
| 30 |
+
# Windows you're weird. (And on Windows setting SNDBUF to 0 makes send
|
| 31 |
+
# blocking, even on non-blocking sockets, so don't do that.)
|
| 32 |
+
self._receive.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1)
|
| 33 |
+
self._send.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1)
|
| 34 |
+
# On Windows this is a TCP socket so this might matter. On other
|
| 35 |
+
# platforms this fails b/c AF_UNIX sockets aren't actually TCP.
|
| 36 |
+
try:
|
| 37 |
+
self._send.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
| 38 |
+
except OSError:
|
| 39 |
+
pass
|
| 40 |
+
|
| 41 |
+
self._selector.register(self._receive, EVENT_READ)
|
| 42 |
+
self._closed = False
|
| 43 |
+
|
| 44 |
+
def start(self) -> None:
|
| 45 |
+
self._thread.start()
|
| 46 |
+
threading._register_atexit(self._stop) # type: ignore[attr-defined]
|
| 47 |
+
|
| 48 |
+
def _stop(self) -> None:
|
| 49 |
+
global _selector
|
| 50 |
+
self._closed = True
|
| 51 |
+
self._notify_self()
|
| 52 |
+
self._send.close()
|
| 53 |
+
self._thread.join()
|
| 54 |
+
self._selector.unregister(self._receive)
|
| 55 |
+
self._receive.close()
|
| 56 |
+
self._selector.close()
|
| 57 |
+
_selector = None
|
| 58 |
+
assert not self._selector.get_map(), (
|
| 59 |
+
"selector still has registered file descriptors after shutdown"
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
def _notify_self(self) -> None:
|
| 63 |
+
try:
|
| 64 |
+
self._send.send(b"\x00")
|
| 65 |
+
except BlockingIOError:
|
| 66 |
+
pass
|
| 67 |
+
|
| 68 |
+
def add_reader(self, fd: FileDescriptorLike, callback: Callable[[], Any]) -> None:
|
| 69 |
+
loop = asyncio.get_running_loop()
|
| 70 |
+
try:
|
| 71 |
+
key = self._selector.get_key(fd)
|
| 72 |
+
except KeyError:
|
| 73 |
+
self._selector.register(fd, EVENT_READ, {EVENT_READ: (loop, callback)})
|
| 74 |
+
else:
|
| 75 |
+
if EVENT_READ in key.data:
|
| 76 |
+
raise ValueError(
|
| 77 |
+
"this file descriptor is already registered for reading"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
key.data[EVENT_READ] = loop, callback
|
| 81 |
+
self._selector.modify(fd, key.events | EVENT_READ, key.data)
|
| 82 |
+
|
| 83 |
+
self._notify_self()
|
| 84 |
+
|
| 85 |
+
def add_writer(self, fd: FileDescriptorLike, callback: Callable[[], Any]) -> None:
|
| 86 |
+
loop = asyncio.get_running_loop()
|
| 87 |
+
try:
|
| 88 |
+
key = self._selector.get_key(fd)
|
| 89 |
+
except KeyError:
|
| 90 |
+
self._selector.register(fd, EVENT_WRITE, {EVENT_WRITE: (loop, callback)})
|
| 91 |
+
else:
|
| 92 |
+
if EVENT_WRITE in key.data:
|
| 93 |
+
raise ValueError(
|
| 94 |
+
"this file descriptor is already registered for writing"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
key.data[EVENT_WRITE] = loop, callback
|
| 98 |
+
self._selector.modify(fd, key.events | EVENT_WRITE, key.data)
|
| 99 |
+
|
| 100 |
+
self._notify_self()
|
| 101 |
+
|
| 102 |
+
def remove_reader(self, fd: FileDescriptorLike) -> bool:
|
| 103 |
+
try:
|
| 104 |
+
key = self._selector.get_key(fd)
|
| 105 |
+
except KeyError:
|
| 106 |
+
return False
|
| 107 |
+
|
| 108 |
+
if new_events := key.events ^ EVENT_READ:
|
| 109 |
+
del key.data[EVENT_READ]
|
| 110 |
+
self._selector.modify(fd, new_events, key.data)
|
| 111 |
+
else:
|
| 112 |
+
self._selector.unregister(fd)
|
| 113 |
+
|
| 114 |
+
return True
|
| 115 |
+
|
| 116 |
+
def remove_writer(self, fd: FileDescriptorLike) -> bool:
|
| 117 |
+
try:
|
| 118 |
+
key = self._selector.get_key(fd)
|
| 119 |
+
except KeyError:
|
| 120 |
+
return False
|
| 121 |
+
|
| 122 |
+
if new_events := key.events ^ EVENT_WRITE:
|
| 123 |
+
del key.data[EVENT_WRITE]
|
| 124 |
+
self._selector.modify(fd, new_events, key.data)
|
| 125 |
+
else:
|
| 126 |
+
self._selector.unregister(fd)
|
| 127 |
+
|
| 128 |
+
return True
|
| 129 |
+
|
| 130 |
+
def run(self) -> None:
|
| 131 |
+
while not self._closed:
|
| 132 |
+
for key, events in self._selector.select():
|
| 133 |
+
if key.fileobj is self._receive:
|
| 134 |
+
try:
|
| 135 |
+
while self._receive.recv(4096):
|
| 136 |
+
pass
|
| 137 |
+
except BlockingIOError:
|
| 138 |
+
pass
|
| 139 |
+
|
| 140 |
+
continue
|
| 141 |
+
|
| 142 |
+
if events & EVENT_READ:
|
| 143 |
+
loop, callback = key.data[EVENT_READ]
|
| 144 |
+
self.remove_reader(key.fd)
|
| 145 |
+
try:
|
| 146 |
+
loop.call_soon_threadsafe(callback)
|
| 147 |
+
except RuntimeError:
|
| 148 |
+
pass # the loop was already closed
|
| 149 |
+
|
| 150 |
+
if events & EVENT_WRITE:
|
| 151 |
+
loop, callback = key.data[EVENT_WRITE]
|
| 152 |
+
self.remove_writer(key.fd)
|
| 153 |
+
try:
|
| 154 |
+
loop.call_soon_threadsafe(callback)
|
| 155 |
+
except RuntimeError:
|
| 156 |
+
pass # the loop was already closed
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def get_selector() -> Selector:
|
| 160 |
+
global _selector
|
| 161 |
+
|
| 162 |
+
with _selector_lock:
|
| 163 |
+
if _selector is None:
|
| 164 |
+
_selector = Selector()
|
| 165 |
+
_selector.start()
|
| 166 |
+
|
| 167 |
+
return _selector
|
env/lib/python3.13/site-packages/anyio/_core/_contextmanagers.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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| 4 |
+
from contextlib import AbstractAsyncContextManager, AbstractContextManager
|
| 5 |
+
from inspect import isasyncgen, iscoroutine, isgenerator
|
| 6 |
+
from types import TracebackType
|
| 7 |
+
from typing import Protocol, TypeVar, cast, final
|
| 8 |
+
|
| 9 |
+
_T_co = TypeVar("_T_co", covariant=True)
|
| 10 |
+
_ExitT_co = TypeVar("_ExitT_co", covariant=True, bound="bool | None")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class _SupportsCtxMgr(Protocol[_T_co, _ExitT_co]):
|
| 14 |
+
def __contextmanager__(self) -> AbstractContextManager[_T_co, _ExitT_co]: ...
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class _SupportsAsyncCtxMgr(Protocol[_T_co, _ExitT_co]):
|
| 18 |
+
def __asynccontextmanager__(
|
| 19 |
+
self,
|
| 20 |
+
) -> AbstractAsyncContextManager[_T_co, _ExitT_co]: ...
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class ContextManagerMixin:
|
| 24 |
+
"""
|
| 25 |
+
Mixin class providing context manager functionality via a generator-based
|
| 26 |
+
implementation.
|
| 27 |
+
|
| 28 |
+
This class allows you to implement a context manager via :meth:`__contextmanager__`
|
| 29 |
+
which should return a generator. The mechanics are meant to mirror those of
|
| 30 |
+
:func:`@contextmanager <contextlib.contextmanager>`.
|
| 31 |
+
|
| 32 |
+
.. note:: Classes using this mix-in are not reentrant as context managers, meaning
|
| 33 |
+
that once you enter it, you can't re-enter before first exiting it.
|
| 34 |
+
|
| 35 |
+
.. seealso:: :doc:`contextmanagers`
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
__cm: AbstractContextManager[object, bool | None] | None = None
|
| 39 |
+
|
| 40 |
+
@final
|
| 41 |
+
def __enter__(self: _SupportsCtxMgr[_T_co, bool | None]) -> _T_co:
|
| 42 |
+
# Needed for mypy to assume self still has the __cm member
|
| 43 |
+
assert isinstance(self, ContextManagerMixin)
|
| 44 |
+
if self.__cm is not None:
|
| 45 |
+
raise RuntimeError(
|
| 46 |
+
f"this {self.__class__.__qualname__} has already been entered"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
cm = self.__contextmanager__()
|
| 50 |
+
if not isinstance(cm, AbstractContextManager):
|
| 51 |
+
if isgenerator(cm):
|
| 52 |
+
raise TypeError(
|
| 53 |
+
"__contextmanager__() returned a generator object instead of "
|
| 54 |
+
"a context manager. Did you forget to add the @contextmanager "
|
| 55 |
+
"decorator?"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
raise TypeError(
|
| 59 |
+
f"__contextmanager__() did not return a context manager object, "
|
| 60 |
+
f"but {cm.__class__!r}"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
if cm is self:
|
| 64 |
+
raise TypeError(
|
| 65 |
+
f"{self.__class__.__qualname__}.__contextmanager__() returned "
|
| 66 |
+
f"self. Did you forget to add the @contextmanager decorator and a "
|
| 67 |
+
f"'yield' statement?"
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
value = cm.__enter__()
|
| 71 |
+
self.__cm = cm
|
| 72 |
+
return value
|
| 73 |
+
|
| 74 |
+
@final
|
| 75 |
+
def __exit__(
|
| 76 |
+
self: _SupportsCtxMgr[object, _ExitT_co],
|
| 77 |
+
exc_type: type[BaseException] | None,
|
| 78 |
+
exc_val: BaseException | None,
|
| 79 |
+
exc_tb: TracebackType | None,
|
| 80 |
+
) -> _ExitT_co:
|
| 81 |
+
# Needed for mypy to assume self still has the __cm member
|
| 82 |
+
assert isinstance(self, ContextManagerMixin)
|
| 83 |
+
if self.__cm is None:
|
| 84 |
+
raise RuntimeError(
|
| 85 |
+
f"this {self.__class__.__qualname__} has not been entered yet"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Prevent circular references
|
| 89 |
+
cm = self.__cm
|
| 90 |
+
del self.__cm
|
| 91 |
+
|
| 92 |
+
return cast(_ExitT_co, cm.__exit__(exc_type, exc_val, exc_tb))
|
| 93 |
+
|
| 94 |
+
@abstractmethod
|
| 95 |
+
def __contextmanager__(self) -> AbstractContextManager[object, bool | None]:
|
| 96 |
+
"""
|
| 97 |
+
Implement your context manager logic here.
|
| 98 |
+
|
| 99 |
+
This method **must** be decorated with
|
| 100 |
+
:func:`@contextmanager <contextlib.contextmanager>`.
|
| 101 |
+
|
| 102 |
+
.. note:: Remember that the ``yield`` will raise any exception raised in the
|
| 103 |
+
enclosed context block, so use a ``finally:`` block to clean up resources!
|
| 104 |
+
|
| 105 |
+
:return: a context manager object
|
| 106 |
+
"""
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class AsyncContextManagerMixin:
|
| 110 |
+
"""
|
| 111 |
+
Mixin class providing async context manager functionality via a generator-based
|
| 112 |
+
implementation.
|
| 113 |
+
|
| 114 |
+
This class allows you to implement a context manager via
|
| 115 |
+
:meth:`__asynccontextmanager__`. The mechanics are meant to mirror those of
|
| 116 |
+
:func:`@asynccontextmanager <contextlib.asynccontextmanager>`.
|
| 117 |
+
|
| 118 |
+
.. note:: Classes using this mix-in are not reentrant as context managers, meaning
|
| 119 |
+
that once you enter it, you can't re-enter before first exiting it.
|
| 120 |
+
|
| 121 |
+
.. seealso:: :doc:`contextmanagers`
|
| 122 |
+
"""
|
| 123 |
+
|
| 124 |
+
__cm: AbstractAsyncContextManager[object, bool | None] | None = None
|
| 125 |
+
|
| 126 |
+
@final
|
| 127 |
+
async def __aenter__(self: _SupportsAsyncCtxMgr[_T_co, bool | None]) -> _T_co:
|
| 128 |
+
# Needed for mypy to assume self still has the __cm member
|
| 129 |
+
assert isinstance(self, AsyncContextManagerMixin)
|
| 130 |
+
if self.__cm is not None:
|
| 131 |
+
raise RuntimeError(
|
| 132 |
+
f"this {self.__class__.__qualname__} has already been entered"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
cm = self.__asynccontextmanager__()
|
| 136 |
+
if not isinstance(cm, AbstractAsyncContextManager):
|
| 137 |
+
if isasyncgen(cm):
|
| 138 |
+
raise TypeError(
|
| 139 |
+
"__asynccontextmanager__() returned an async generator instead of "
|
| 140 |
+
"an async context manager. Did you forget to add the "
|
| 141 |
+
"@asynccontextmanager decorator?"
|
| 142 |
+
)
|
| 143 |
+
elif iscoroutine(cm):
|
| 144 |
+
cm.close()
|
| 145 |
+
raise TypeError(
|
| 146 |
+
"__asynccontextmanager__() returned a coroutine object instead of "
|
| 147 |
+
"an async context manager. Did you forget to add the "
|
| 148 |
+
"@asynccontextmanager decorator and a 'yield' statement?"
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
raise TypeError(
|
| 152 |
+
f"__asynccontextmanager__() did not return an async context manager, "
|
| 153 |
+
f"but {cm.__class__!r}"
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
if cm is self:
|
| 157 |
+
raise TypeError(
|
| 158 |
+
f"{self.__class__.__qualname__}.__asynccontextmanager__() returned "
|
| 159 |
+
f"self. Did you forget to add the @asynccontextmanager decorator and a "
|
| 160 |
+
f"'yield' statement?"
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
value = await cm.__aenter__()
|
| 164 |
+
self.__cm = cm
|
| 165 |
+
return value
|
| 166 |
+
|
| 167 |
+
@final
|
| 168 |
+
async def __aexit__(
|
| 169 |
+
self: _SupportsAsyncCtxMgr[object, _ExitT_co],
|
| 170 |
+
exc_type: type[BaseException] | None,
|
| 171 |
+
exc_val: BaseException | None,
|
| 172 |
+
exc_tb: TracebackType | None,
|
| 173 |
+
) -> _ExitT_co:
|
| 174 |
+
assert isinstance(self, AsyncContextManagerMixin)
|
| 175 |
+
if self.__cm is None:
|
| 176 |
+
raise RuntimeError(
|
| 177 |
+
f"this {self.__class__.__qualname__} has not been entered yet"
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# Prevent circular references
|
| 181 |
+
cm = self.__cm
|
| 182 |
+
del self.__cm
|
| 183 |
+
|
| 184 |
+
return cast(_ExitT_co, await cm.__aexit__(exc_type, exc_val, exc_tb))
|
| 185 |
+
|
| 186 |
+
@abstractmethod
|
| 187 |
+
def __asynccontextmanager__(
|
| 188 |
+
self,
|
| 189 |
+
) -> AbstractAsyncContextManager[object, bool | None]:
|
| 190 |
+
"""
|
| 191 |
+
Implement your async context manager logic here.
|
| 192 |
+
|
| 193 |
+
This method **must** be decorated with
|
| 194 |
+
:func:`@asynccontextmanager <contextlib.asynccontextmanager>`.
|
| 195 |
+
|
| 196 |
+
.. note:: Remember that the ``yield`` will raise any exception raised in the
|
| 197 |
+
enclosed context block, so use a ``finally:`` block to clean up resources!
|
| 198 |
+
|
| 199 |
+
:return: an async context manager object
|
| 200 |
+
"""
|
env/lib/python3.13/site-packages/anyio/_core/_eventloop.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import math
|
| 4 |
+
import sys
|
| 5 |
+
import threading
|
| 6 |
+
from collections.abc import Awaitable, Callable, Generator
|
| 7 |
+
from contextlib import contextmanager
|
| 8 |
+
from contextvars import Token
|
| 9 |
+
from importlib import import_module
|
| 10 |
+
from typing import TYPE_CHECKING, Any, TypeVar
|
| 11 |
+
|
| 12 |
+
from ._exceptions import NoEventLoopError
|
| 13 |
+
|
| 14 |
+
if sys.version_info >= (3, 11):
|
| 15 |
+
from typing import TypeVarTuple, Unpack
|
| 16 |
+
else:
|
| 17 |
+
from typing_extensions import TypeVarTuple, Unpack
|
| 18 |
+
|
| 19 |
+
sniffio: Any
|
| 20 |
+
try:
|
| 21 |
+
import sniffio
|
| 22 |
+
except ModuleNotFoundError:
|
| 23 |
+
sniffio = None
|
| 24 |
+
|
| 25 |
+
if TYPE_CHECKING:
|
| 26 |
+
from ..abc import AsyncBackend
|
| 27 |
+
|
| 28 |
+
# This must be updated when new backends are introduced
|
| 29 |
+
BACKENDS = "asyncio", "trio"
|
| 30 |
+
|
| 31 |
+
T_Retval = TypeVar("T_Retval")
|
| 32 |
+
PosArgsT = TypeVarTuple("PosArgsT")
|
| 33 |
+
|
| 34 |
+
threadlocals = threading.local()
|
| 35 |
+
loaded_backends: dict[str, type[AsyncBackend]] = {}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def run(
|
| 39 |
+
func: Callable[[Unpack[PosArgsT]], Awaitable[T_Retval]],
|
| 40 |
+
*args: Unpack[PosArgsT],
|
| 41 |
+
backend: str = "asyncio",
|
| 42 |
+
backend_options: dict[str, Any] | None = None,
|
| 43 |
+
) -> T_Retval:
|
| 44 |
+
"""
|
| 45 |
+
Run the given coroutine function in an asynchronous event loop.
|
| 46 |
+
|
| 47 |
+
The current thread must not be already running an event loop.
|
| 48 |
+
|
| 49 |
+
:param func: a coroutine function
|
| 50 |
+
:param args: positional arguments to ``func``
|
| 51 |
+
:param backend: name of the asynchronous event loop implementation – currently
|
| 52 |
+
either ``asyncio`` or ``trio``
|
| 53 |
+
:param backend_options: keyword arguments to call the backend ``run()``
|
| 54 |
+
implementation with (documented :ref:`here <backend options>`)
|
| 55 |
+
:return: the return value of the coroutine function
|
| 56 |
+
:raises RuntimeError: if an asynchronous event loop is already running in this
|
| 57 |
+
thread
|
| 58 |
+
:raises LookupError: if the named backend is not found
|
| 59 |
+
|
| 60 |
+
"""
|
| 61 |
+
if asynclib_name := current_async_library():
|
| 62 |
+
raise RuntimeError(f"Already running {asynclib_name} in this thread")
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
async_backend = get_async_backend(backend)
|
| 66 |
+
except ImportError as exc:
|
| 67 |
+
raise LookupError(f"No such backend: {backend}") from exc
|
| 68 |
+
|
| 69 |
+
token = None
|
| 70 |
+
if asynclib_name is None:
|
| 71 |
+
# Since we're in control of the event loop, we can cache the name of the async
|
| 72 |
+
# library
|
| 73 |
+
token = set_current_async_library(backend)
|
| 74 |
+
|
| 75 |
+
try:
|
| 76 |
+
backend_options = backend_options or {}
|
| 77 |
+
return async_backend.run(func, args, {}, backend_options)
|
| 78 |
+
finally:
|
| 79 |
+
reset_current_async_library(token)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
async def sleep(delay: float) -> None:
|
| 83 |
+
"""
|
| 84 |
+
Pause the current task for the specified duration.
|
| 85 |
+
|
| 86 |
+
:param delay: the duration, in seconds
|
| 87 |
+
|
| 88 |
+
"""
|
| 89 |
+
return await get_async_backend().sleep(delay)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
async def sleep_forever() -> None:
|
| 93 |
+
"""
|
| 94 |
+
Pause the current task until it's cancelled.
|
| 95 |
+
|
| 96 |
+
This is a shortcut for ``sleep(math.inf)``.
|
| 97 |
+
|
| 98 |
+
.. versionadded:: 3.1
|
| 99 |
+
|
| 100 |
+
"""
|
| 101 |
+
await sleep(math.inf)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
async def sleep_until(deadline: float) -> None:
|
| 105 |
+
"""
|
| 106 |
+
Pause the current task until the given time.
|
| 107 |
+
|
| 108 |
+
:param deadline: the absolute time to wake up at (according to the internal
|
| 109 |
+
monotonic clock of the event loop)
|
| 110 |
+
|
| 111 |
+
.. versionadded:: 3.1
|
| 112 |
+
|
| 113 |
+
"""
|
| 114 |
+
now = current_time()
|
| 115 |
+
await sleep(max(deadline - now, 0))
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def current_time() -> float:
|
| 119 |
+
"""
|
| 120 |
+
Return the current value of the event loop's internal clock.
|
| 121 |
+
|
| 122 |
+
:return: the clock value (seconds)
|
| 123 |
+
:raises NoEventLoopError: if no supported asynchronous event loop is running in the
|
| 124 |
+
current thread
|
| 125 |
+
|
| 126 |
+
"""
|
| 127 |
+
return get_async_backend().current_time()
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def get_all_backends() -> tuple[str, ...]:
|
| 131 |
+
"""Return a tuple of the names of all built-in backends."""
|
| 132 |
+
return BACKENDS
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def get_available_backends() -> tuple[str, ...]:
|
| 136 |
+
"""
|
| 137 |
+
Test for the availability of built-in backends.
|
| 138 |
+
|
| 139 |
+
:return a tuple of the built-in backend names that were successfully imported
|
| 140 |
+
|
| 141 |
+
.. versionadded:: 4.12
|
| 142 |
+
|
| 143 |
+
"""
|
| 144 |
+
available_backends: list[str] = []
|
| 145 |
+
for backend_name in get_all_backends():
|
| 146 |
+
try:
|
| 147 |
+
get_async_backend(backend_name)
|
| 148 |
+
except ImportError:
|
| 149 |
+
continue
|
| 150 |
+
|
| 151 |
+
available_backends.append(backend_name)
|
| 152 |
+
|
| 153 |
+
return tuple(available_backends)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def get_cancelled_exc_class() -> type[BaseException]:
|
| 157 |
+
"""
|
| 158 |
+
Return the current async library's cancellation exception class.
|
| 159 |
+
|
| 160 |
+
:raises NoEventLoopError: if no supported asynchronous event loop is running in the
|
| 161 |
+
current thread
|
| 162 |
+
|
| 163 |
+
"""
|
| 164 |
+
return get_async_backend().cancelled_exception_class()
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
#
|
| 168 |
+
# Private API
|
| 169 |
+
#
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
@contextmanager
|
| 173 |
+
def claim_worker_thread(
|
| 174 |
+
backend_class: type[AsyncBackend], token: object
|
| 175 |
+
) -> Generator[Any, None, None]:
|
| 176 |
+
from ..lowlevel import EventLoopToken
|
| 177 |
+
|
| 178 |
+
threadlocals.current_token = EventLoopToken(backend_class, token)
|
| 179 |
+
try:
|
| 180 |
+
yield
|
| 181 |
+
finally:
|
| 182 |
+
del threadlocals.current_token
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def get_async_backend(asynclib_name: str | None = None) -> type[AsyncBackend]:
|
| 186 |
+
if asynclib_name is None:
|
| 187 |
+
asynclib_name = current_async_library()
|
| 188 |
+
if not asynclib_name:
|
| 189 |
+
raise NoEventLoopError(
|
| 190 |
+
f"Not currently running on any asynchronous event loop. "
|
| 191 |
+
f"Available async backends: {', '.join(get_all_backends())}"
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
# We use our own dict instead of sys.modules to get the already imported back-end
|
| 195 |
+
# class because the appropriate modules in sys.modules could potentially be only
|
| 196 |
+
# partially initialized
|
| 197 |
+
try:
|
| 198 |
+
return loaded_backends[asynclib_name]
|
| 199 |
+
except KeyError:
|
| 200 |
+
module = import_module(f"anyio._backends._{asynclib_name}")
|
| 201 |
+
loaded_backends[asynclib_name] = module.backend_class
|
| 202 |
+
return module.backend_class
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def current_async_library() -> str | None:
|
| 206 |
+
if sniffio is None:
|
| 207 |
+
# If sniffio is not installed, we assume we're either running asyncio or nothing
|
| 208 |
+
import asyncio
|
| 209 |
+
|
| 210 |
+
try:
|
| 211 |
+
asyncio.get_running_loop()
|
| 212 |
+
return "asyncio"
|
| 213 |
+
except RuntimeError:
|
| 214 |
+
pass
|
| 215 |
+
else:
|
| 216 |
+
try:
|
| 217 |
+
return sniffio.current_async_library()
|
| 218 |
+
except sniffio.AsyncLibraryNotFoundError:
|
| 219 |
+
pass
|
| 220 |
+
|
| 221 |
+
return None
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
def set_current_async_library(asynclib_name: str | None) -> Token | None:
|
| 225 |
+
# no-op if sniffio is not installed
|
| 226 |
+
if sniffio is None:
|
| 227 |
+
return None
|
| 228 |
+
|
| 229 |
+
return sniffio.current_async_library_cvar.set(asynclib_name)
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
def reset_current_async_library(token: Token | None) -> None:
|
| 233 |
+
if token is not None:
|
| 234 |
+
sniffio.current_async_library_cvar.reset(token)
|