diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/asyncio/tasks.py | 4 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_tasks.py | 15 |
2 files changed, 19 insertions, 0 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 8d5bde0..4250bb0 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -15,6 +15,7 @@ import contextvars import functools import inspect import itertools +import math import types import warnings import weakref @@ -646,6 +647,9 @@ async def sleep(delay, result=None): await __sleep0() return result + if math.isnan(delay): + raise ValueError("Invalid delay: NaN (not a number)") + loop = events.get_running_loop() future = loop.create_future() h = loop.call_later(delay, diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 6e8a51c..4dfaff8 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -1609,6 +1609,21 @@ class BaseTaskTests: self.assertEqual(t.result(), 'yeah') self.assertAlmostEqual(0.1, loop.time()) + def test_sleep_when_delay_is_nan(self): + + def gen(): + yield + + loop = self.new_test_loop(gen) + + async def sleeper(): + await asyncio.sleep(float("nan")) + + t = self.new_task(loop, sleeper()) + + with self.assertRaises(ValueError): + loop.run_until_complete(t) + def test_sleep_cancel(self): def gen(): |