Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3578.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Trio will no longer accept `math.nan` in many places where floats are accepted.
13 changes: 10 additions & 3 deletions src/trio/_core/_mock_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,27 @@ 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:
return self._autojump_threshold

@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
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions src/trio/_core/_tests/test_mock_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/trio/_dtls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/trio/_highlevel_open_tcp_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
from contextlib import contextmanager, suppress
from math import isnan
from typing import TYPE_CHECKING

import trio
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions src/trio/_tests/test_dtls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/trio/_tests/test_highlevel_open_tcp_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/trio/_tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))


################################################################


Expand Down
Loading