Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 20, 2025
1 parent 3a0fb1c commit c8357a0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
5 changes: 1 addition & 4 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@
set_result,
)
from .resolver import DefaultResolver
from .tcp_helpers import (
_SOCKOPT,
tcp_setsockopt,
)
from .tcp_helpers import _SOCKOPT, tcp_setsockopt

if TYPE_CHECKING:
import ssl
Expand Down
8 changes: 2 additions & 6 deletions aiohttp/tcp_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import asyncio
import socket
from contextlib import suppress
from typing import (
Any,
List,
Optional,
Tuple,
)
from typing import Any, List, Optional, Tuple

__all__ = ("tcp_keepalive", "tcp_nodelay", "tcp_setsockopt")

Expand Down Expand Up @@ -42,6 +37,7 @@ def tcp_nodelay(transport: asyncio.Transport, value: bool) -> None:
with suppress(OSError):
tcp_setsockopt(sock, [(socket.IPPROTO_TCP, socket.TCP_NODELAY, value)])


def tcp_setsockopt(sock: Optional[socket.socket], sockopts: List[_SOCKOPT]) -> None:
if sock is not None:
for sockopt in sockopts:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3748,7 +3748,9 @@ async def test_tcp_connector_setsockopts(
loop: asyncio.AbstractEventLoop, start_connection: mock.AsyncMock
) -> None:
"""Check that sockopts get passed to socket"""
conn = aiohttp.TCPConnector(tcp_sockopts=[(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 2)])
conn = aiohttp.TCPConnector(
tcp_sockopts=[(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 2)]
)

with mock.patch.object(
conn._loop, "create_connection", autospec=True, spec_set=True
Expand Down
18 changes: 14 additions & 4 deletions tests/test_tcp_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,21 @@ def test_tcp_nodelay_enable_no_socket() -> None:

def test_tcp_setsockopts() -> None:
# Should do nothing
tcp_setsockopt(None, [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True),
(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 20)])
tcp_setsockopt(
None,
[
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True),
(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 20),
],
)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
tcp_setsockopt(s, [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True),
(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10)])
tcp_setsockopt(
s,
[
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True),
(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10),
],
)
assert s.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE)
assert s.getsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL) == 10

0 comments on commit c8357a0

Please sign in to comment.