Voice et bot modif

This commit is contained in:
pi 2026-06-16 17:09:34 +00:00
parent 189d56026b
commit 7333a22bcd
10774 changed files with 634644 additions and 933308 deletions

View file

@ -1,4 +1,4 @@
__version__ = "2.6.1"
__version__ = "2.6.2"
from .impl import start_connection
from .types import AddrInfoType, SocketFactoryType

View file

@ -1,20 +1,10 @@
import asyncio
import contextlib
# PY3.9: Import Callable from typing until we drop Python 3.9 support
# https://github.com/python/cpython/issues/87131
from collections.abc import Awaitable, Callable, Iterable
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
Iterable,
List,
Optional,
Set,
Tuple,
TypeVar,
Union,
)
_T = TypeVar("_T")
@ -51,10 +41,10 @@ async def _wait_one(
async def staggered_race(
coro_fns: Iterable[Callable[[], Awaitable[_T]]],
delay: Optional[float],
delay: float | None,
*,
loop: Optional[asyncio.AbstractEventLoop] = None,
) -> Tuple[Optional[_T], Optional[int], List[Optional[BaseException]]]:
loop: asyncio.AbstractEventLoop | None = None,
) -> tuple[_T | None, int | None, list[BaseException | None]]:
"""
Run coroutines with staggered start times and take the first to finish.
@ -109,14 +99,14 @@ async def staggered_race(
"""
loop = loop or asyncio.get_running_loop()
exceptions: List[Optional[BaseException]] = []
tasks: Set[asyncio.Task[Optional[Tuple[_T, int]]]] = set()
exceptions: list[BaseException | None] = []
tasks: set[asyncio.Task[tuple[_T, int] | None]] = set()
async def run_one_coro(
coro_fn: Callable[[], Awaitable[_T]],
this_index: int,
start_next: "asyncio.Future[None]",
) -> Optional[Tuple[_T, int]]:
) -> tuple[_T, int] | None:
"""
Run a single coroutine.
@ -139,10 +129,10 @@ async def staggered_race(
return result, this_index
start_next_timer: Optional[asyncio.TimerHandle] = None
start_next: Optional[asyncio.Future[None]]
task: asyncio.Task[Optional[Tuple[_T, int]]]
done: Union[asyncio.Future[None], asyncio.Task[Optional[Tuple[_T, int]]]]
start_next_timer: asyncio.TimerHandle | None = None
start_next: asyncio.Future[None] | None
task: asyncio.Task[tuple[_T, int] | None]
done: asyncio.Future[None] | asyncio.Task[tuple[_T, int] | None]
coro_iter = iter(coro_fns)
this_index = -1
try:

View file

@ -6,7 +6,7 @@ import contextlib
import functools
import itertools
import socket
from typing import List, Optional, Sequence, Set, Union
from collections.abc import Sequence
from . import _staggered
from .types import AddrInfoType, SocketFactoryType
@ -15,11 +15,11 @@ from .types import AddrInfoType, SocketFactoryType
async def start_connection(
addr_infos: Sequence[AddrInfoType],
*,
local_addr_infos: Optional[Sequence[AddrInfoType]] = None,
happy_eyeballs_delay: Optional[float] = None,
interleave: Optional[int] = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
socket_factory: Optional[SocketFactoryType] = None,
local_addr_infos: Sequence[AddrInfoType] | None = None,
happy_eyeballs_delay: float | None = None,
interleave: int | None = None,
loop: asyncio.AbstractEventLoop | None = None,
socket_factory: SocketFactoryType | None = None,
) -> socket.socket:
"""
Connect to a TCP server.
@ -51,8 +51,10 @@ async def start_connection(
transport, protocol = await loop.create_connection(
MyProtocol, sock=socket, ...)
"""
if not (current_loop := loop):
current_loop = asyncio.get_running_loop()
if not addr_infos:
raise ValueError("addr_infos must not be empty")
current_loop = loop or asyncio.get_running_loop()
single_addr_info = len(addr_infos) == 1
@ -63,9 +65,9 @@ async def start_connection(
if interleave and not single_addr_info:
addr_infos = _interleave_addrinfos(addr_infos, interleave)
sock: Optional[socket.socket] = None
sock: socket.socket | None = None
# uvloop can raise RuntimeError instead of OSError
exceptions: List[List[Union[OSError, RuntimeError]]] = []
exceptions: list[list[OSError | RuntimeError]] = []
if happy_eyeballs_delay is None or single_addr_info:
# not using happy eyeballs
for addrinfo in addr_infos:
@ -82,7 +84,7 @@ async def start_connection(
except (RuntimeError, OSError):
continue
else: # using happy eyeballs
open_sockets: Set[socket.socket] = set()
open_sockets: set[socket.socket] = set()
try:
sock, _, _ = await _staggered.staggered_race(
(
@ -156,11 +158,11 @@ async def start_connection(
async def _connect_sock(
loop: asyncio.AbstractEventLoop,
exceptions: List[List[Union[OSError, RuntimeError]]],
exceptions: list[list[OSError | RuntimeError]],
addr_info: AddrInfoType,
local_addr_infos: Optional[Sequence[AddrInfoType]] = None,
open_sockets: Optional[Set[socket.socket]] = None,
socket_factory: Optional[SocketFactoryType] = None,
local_addr_infos: Sequence[AddrInfoType] | None = None,
open_sockets: set[socket.socket] | None = None,
socket_factory: SocketFactoryType | None = None,
) -> socket.socket:
"""
Create, bind and connect one socket.
@ -172,7 +174,7 @@ async def _connect_sock(
of all staggered tasks in the result there are runner up sockets aka
multiple winners.
"""
my_exceptions: List[Union[OSError, RuntimeError]] = []
my_exceptions: list[OSError | RuntimeError] = []
exceptions.append(my_exceptions)
family, type_, proto, _, address = addr_info
sock = None
@ -234,10 +236,10 @@ async def _connect_sock(
def _interleave_addrinfos(
addrinfos: Sequence[AddrInfoType], first_address_family_count: int = 1
) -> List[AddrInfoType]:
) -> list[AddrInfoType]:
"""Interleave list of addrinfo tuples by family."""
# Group addresses by family
addrinfos_by_family: collections.OrderedDict[int, List[AddrInfoType]] = (
addrinfos_by_family: collections.OrderedDict[int, list[AddrInfoType]] = (
collections.OrderedDict()
)
for addr in addrinfos:
@ -247,7 +249,7 @@ def _interleave_addrinfos(
addrinfos_by_family[family].append(addr)
addrinfos_lists = list(addrinfos_by_family.values())
reordered: List[AddrInfoType] = []
reordered: list[AddrInfoType] = []
if first_address_family_count > 1:
reordered.extend(addrinfos_lists[0][: first_address_family_count - 1])
del addrinfos_lists[0][: first_address_family_count - 1]

View file

@ -1,17 +1,14 @@
"""Types for aiohappyeyeballs."""
import socket
from collections.abc import Callable
# PY3.9: Import Callable from typing until we drop Python 3.9 support
# https://github.com/python/cpython/issues/87131
from typing import Callable, Tuple, Union
AddrInfoType = Tuple[
Union[int, socket.AddressFamily],
Union[int, socket.SocketKind],
AddrInfoType = tuple[
int | socket.AddressFamily,
int | socket.SocketKind,
int,
str,
Tuple, # type: ignore[type-arg]
tuple, # type: ignore[type-arg]
]
SocketFactoryType = Callable[[AddrInfoType], socket.socket]

View file

@ -2,16 +2,13 @@
import ipaddress
import socket
from typing import Dict, List, Optional, Tuple, Union
from .types import AddrInfoType
def addr_to_addr_infos(
addr: Optional[
Union[Tuple[str, int, int, int], Tuple[str, int, int], Tuple[str, int]]
],
) -> Optional[List[AddrInfoType]]:
addr: tuple[str, int, int, int] | tuple[str, int, int] | tuple[str, int] | None,
) -> list[AddrInfoType] | None:
"""Convert an address tuple to a list of addr_info tuples."""
if addr is None:
return None
@ -35,7 +32,7 @@ def addr_to_addr_infos(
def pop_addr_infos_interleave(
addr_infos: List[AddrInfoType], interleave: Optional[int] = None
addr_infos: list[AddrInfoType], interleave: int | None = None
) -> None:
"""
Pop addr_info from the list of addr_infos by family up to interleave times.
@ -43,10 +40,10 @@ def pop_addr_infos_interleave(
The interleave parameter is used to know how many addr_infos for
each family should be popped of the top of the list.
"""
seen: Dict[int, int] = {}
seen: dict[int, int] = {}
if interleave is None:
interleave = 1
to_remove: List[AddrInfoType] = []
to_remove: list[AddrInfoType] = []
for addr_info in addr_infos:
family = addr_info[0]
if family not in seen:
@ -59,17 +56,15 @@ def pop_addr_infos_interleave(
def _addr_tuple_to_ip_address(
addr: Union[Tuple[str, int], Tuple[str, int, int, int]],
) -> Union[
Tuple[ipaddress.IPv4Address, int], Tuple[ipaddress.IPv6Address, int, int, int]
]:
addr: tuple[str, int] | tuple[str, int, int, int],
) -> tuple[ipaddress.IPv4Address, int] | tuple[ipaddress.IPv6Address, int, int, int]:
"""Convert an address tuple to an IPv4Address."""
return (ipaddress.ip_address(addr[0]), *addr[1:])
def remove_addr_infos(
addr_infos: List[AddrInfoType],
addr: Union[Tuple[str, int], Tuple[str, int, int, int]],
addr_infos: list[AddrInfoType],
addr: tuple[str, int] | tuple[str, int, int, int],
) -> None:
"""
Remove an address from the list of addr_infos.
@ -77,7 +72,7 @@ def remove_addr_infos(
The addr value is typically the return value of
sock.getpeername().
"""
bad_addrs_infos: List[AddrInfoType] = []
bad_addrs_infos: list[AddrInfoType] = []
for addr_info in addr_infos:
if addr_info[-1] == addr:
bad_addrs_infos.append(addr_info)