Skip to content

Commit

Permalink
Change warning to use warn_deprecated and add corrosponding test fo…
Browse files Browse the repository at this point in the history
…r this change
  • Loading branch information
CoolCat467 committed Oct 30, 2023
1 parent f815fb3 commit 60f6956
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
10 changes: 6 additions & 4 deletions trio/_highlevel_open_tcp_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import errno
import math
import sys
import warnings
from collections.abc import Awaitable, Callable

import trio
from trio import TaskStatus

from . import socket as tsocket
from ._deprecate import warn_deprecated

if sys.version_info < (3, 11):
from exceptiongroup import ExceptionGroup
Expand Down Expand Up @@ -48,9 +48,11 @@ def _compute_backlog(backlog: int | None) -> int:
# https://github.com/golang/go/issues/5030
if backlog == math.inf:
backlog = None
warnings.warn(
"Accepting infinity for backlog for compatibility, please use `None` instead.",
stacklevel=2,
warn_deprecated(
thing="math.inf as a backlog",
version="0.23.0",
instead="None",
issue=2842,
)
if not isinstance(backlog, int) and backlog is not None:
raise TypeError(f"backlog must be an int or None, not {backlog!r}")
Expand Down
19 changes: 18 additions & 1 deletion trio/_tests/test_highlevel_open_tcp_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
import pytest

import trio
from trio import SocketListener, open_tcp_listeners, open_tcp_stream, serve_tcp
from trio import (
SocketListener,
TrioDeprecationWarning,
open_tcp_listeners,
open_tcp_stream,
serve_tcp,
)
from trio.abc import HostnameResolver, SendStream, SocketFactory
from trio.testing import open_stream_to_socket_listener

Expand Down Expand Up @@ -378,6 +384,17 @@ async def test_open_tcp_listeners_backlog() -> None:
assert listener.socket.backlog == expected # type: ignore[attr-defined]


async def test_open_tcp_listeners_backlog_inf_warning() -> None:
fsf = FakeSocketFactory(99)
tsocket.set_custom_socket_factory(fsf)
with pytest.warns(TrioDeprecationWarning):
listeners = await open_tcp_listeners(0, backlog=float("inf")) # type: ignore[arg-type]
assert listeners
for listener in listeners:
# `backlog` only exists on FakeSocket
assert listener.socket.backlog == 0xFFFF # type: ignore[attr-defined]


async def test_open_tcp_listeners_backlog_float_error() -> None:
fsf = FakeSocketFactory(99)
tsocket.set_custom_socket_factory(fsf)
Expand Down

0 comments on commit 60f6956

Please sign in to comment.