diff --git a/newsfragments/3578.removal.rst b/newsfragments/3578.removal.rst new file mode 100644 index 0000000000..b55b241992 --- /dev/null +++ b/newsfragments/3578.removal.rst @@ -0,0 +1 @@ +Trio will no longer accept `math.nan` in many places where floats are accepted. diff --git a/src/trio/_core/_mock_clock.py b/src/trio/_core/_mock_clock.py index bc530115f5..8d457a5102 100644 --- a/src/trio/_core/_mock_clock.py +++ b/src/trio/_core/_mock_clock.py @@ -87,14 +87,16 @@ def rate(self) -> float: @rate.setter def rate(self, new_rate: float) -> None: - if isnan(new_rate) or new_rate < 0: + if isnan(new_rate): + raise ValueError("rate must not be NaN") + elif new_rate < 0: raise ValueError(f"rate must be a non-negative number, not {new_rate!r}") else: real = self._real_clock() virtual = self._real_to_virtual(real) self._virtual_base = virtual self._real_base = real - self._rate = float(new_rate) + self._rate = new_rate @property def autojump_threshold(self) -> float: @@ -102,7 +104,10 @@ def autojump_threshold(self) -> float: @autojump_threshold.setter def autojump_threshold(self, new_autojump_threshold: float) -> None: - self._autojump_threshold = float(new_autojump_threshold) + if isnan(new_autojump_threshold): + raise ValueError("autojump_threshold must not be NaN") + + self._autojump_threshold = new_autojump_threshold self._try_resync_autojump_threshold() # runner.clock_autojump_threshold is an internal API that isn't easily @@ -160,6 +165,8 @@ def jump(self, seconds: float) -> None: ValueError: if you try to pass a negative value for ``seconds``. """ + if isnan(seconds): + raise ValueError("time must not be NaN") if seconds < 0: raise ValueError("time can't go backwards") self._virtual_base += seconds diff --git a/src/trio/_core/_run.py b/src/trio/_core/_run.py index ddd4ea7ee2..61f670cab6 100644 --- a/src/trio/_core/_run.py +++ b/src/trio/_core/_run.py @@ -2305,6 +2305,8 @@ async def test_lock_fairness(): print("FAIL") """ + if isnan(cushion): + raise ValueError("cushion must not be NaN") task = current_task() key = (cushion, id(task)) self.waiting_for_idle[key] = task diff --git a/src/trio/_core/_tests/test_mock_clock.py b/src/trio/_core/_tests/test_mock_clock.py index 47ee1ff2a4..ca187d3855 100644 --- a/src/trio/_core/_tests/test_mock_clock.py +++ b/src/trio/_core/_tests/test_mock_clock.py @@ -34,17 +34,23 @@ def test_mock_clock() -> None: c.rate = -1 assert c.rate == 0 - with pytest.raises( - ValueError, match=r"^rate must be a non-negative number, not nan$" - ): + with pytest.raises(ValueError, match=r"^rate must not be NaN$"): c.rate = float("nan") assert c.rate == 0 - with pytest.raises( - ValueError, match=r"^rate must be a non-negative number, not nan$" - ): + with pytest.raises(ValueError, match=r"^rate must not be NaN$"): MockClock(rate=float("nan")) + with pytest.raises(ValueError, match=r"^autojump_threshold must not be NaN$"): + c.autojump_threshold = float("nan") + assert c.autojump_threshold == inf + + with pytest.raises(ValueError, match=r"^autojump_threshold must not be NaN$"): + MockClock(autojump_threshold=float("nan")) + + with pytest.raises(ValueError, match=r"^time must not be NaN$"): + c.jump(float("nan")) + c.rate = 2 assert c.current_time() == 1.2 REAL_NOW += 1 diff --git a/src/trio/_dtls.py b/src/trio/_dtls.py index a789038257..ab27f9cec7 100644 --- a/src/trio/_dtls.py +++ b/src/trio/_dtls.py @@ -17,6 +17,7 @@ import warnings import weakref from itertools import count +from math import isnan from typing import TYPE_CHECKING, Generic, TypeAlias, TypeVar from weakref import ReferenceType, WeakValueDictionary @@ -957,6 +958,8 @@ async def do_handshake(self, *, initial_retransmit_timeout: float = 1.0) -> None network. """ + if isnan(initial_retransmit_timeout): + raise ValueError("timeout must not be NaN") async with self._handshake_lock: if self._did_handshake: return diff --git a/src/trio/_highlevel_open_tcp_stream.py b/src/trio/_highlevel_open_tcp_stream.py index 400bc52834..29c350503c 100644 --- a/src/trio/_highlevel_open_tcp_stream.py +++ b/src/trio/_highlevel_open_tcp_stream.py @@ -2,6 +2,7 @@ import sys from contextlib import contextmanager, suppress +from math import isnan from typing import TYPE_CHECKING import trio @@ -268,6 +269,8 @@ async def open_tcp_stream( if happy_eyeballs_delay is None: happy_eyeballs_delay = DEFAULT_DELAY + if isnan(happy_eyeballs_delay): + raise ValueError("happy eyeballs delay must not be NaN") targets = await getaddrinfo(host, port, type=SOCK_STREAM) diff --git a/src/trio/_tests/test_dtls.py b/src/trio/_tests/test_dtls.py index 5a34d8c811..ea77822197 100644 --- a/src/trio/_tests/test_dtls.py +++ b/src/trio/_tests/test_dtls.py @@ -706,6 +706,15 @@ def route_packet(packet: UDPPacket) -> None: after = trio.current_time() assert after - before == t + with pytest.raises(ValueError, match=r"^timeout must not be NaN$"): + with endpoint() as client: + before = trio.current_time() + blackholed = True + channel = client.connect(address, client_ctx_fn()) + await channel.do_handshake(initial_retransmit_timeout=float("nan")) + after = trio.current_time() + assert after - before == 0 + async def test_explicit_tiny_mtu_is_respected( server_ctx: SSL.Context, client_ctx: SSL.Context diff --git a/src/trio/_tests/test_highlevel_open_tcp_stream.py b/src/trio/_tests/test_highlevel_open_tcp_stream.py index 3c9ae9e593..7aecbdce51 100644 --- a/src/trio/_tests/test_highlevel_open_tcp_stream.py +++ b/src/trio/_tests/test_highlevel_open_tcp_stream.py @@ -509,6 +509,19 @@ async def test_none_default(autojump_clock: MockClock) -> None: } +async def test_happy_eyeballs_nan_delay() -> None: + with pytest.raises(ValueError, match=r"^happy eyeballs delay must not be NaN$"): + await run_scenario( + 80, + [ + ("1.1.1.1", 1, "success"), + ("2.2.2.2", 1, "success"), + ("3.3.3.3", 0.2, "success"), + ], + happy_eyeballs_delay=float("nan"), + ) + + async def test_custom_errors_expedite(autojump_clock: MockClock) -> None: sock, scenario = await run_scenario( 80, diff --git a/src/trio/_tests/test_testing.py b/src/trio/_tests/test_testing.py index 7093a6187e..cdb73fda4c 100644 --- a/src/trio/_tests/test_testing.py +++ b/src/trio/_tests/test_testing.py @@ -107,6 +107,11 @@ async def wait_big_cushion() -> None: ] +async def test_wait_all_tasks_blocked_with_nan_cushion() -> None: + with pytest.raises(ValueError, match=r"^cushion must not be NaN$"): + await wait_all_tasks_blocked(float("nan")) + + ################################################################