mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-105331: Change asyncio.sleep
to raise `ValueError
for nan (#105641)
Co-authored-by: Guido van Rossum <gvanrossum@gmail.com> Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
This commit is contained in:
parent
9544948e7e
commit
f0fb782ddb
4 changed files with 24 additions and 0 deletions
|
@ -426,6 +426,9 @@ Sleeping
|
||||||
.. versionchanged:: 3.10
|
.. versionchanged:: 3.10
|
||||||
Removed the *loop* parameter.
|
Removed the *loop* parameter.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.13
|
||||||
|
Raises :exc:`ValueError` if *delay* is :data:`~math.nan`.
|
||||||
|
|
||||||
|
|
||||||
Running Tasks Concurrently
|
Running Tasks Concurrently
|
||||||
==========================
|
==========================
|
||||||
|
|
|
@ -15,6 +15,7 @@ import contextvars
|
||||||
import functools
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
import itertools
|
import itertools
|
||||||
|
import math
|
||||||
import types
|
import types
|
||||||
import warnings
|
import warnings
|
||||||
import weakref
|
import weakref
|
||||||
|
@ -646,6 +647,9 @@ async def sleep(delay, result=None):
|
||||||
await __sleep0()
|
await __sleep0()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
if math.isnan(delay):
|
||||||
|
raise ValueError("Invalid delay: NaN (not a number)")
|
||||||
|
|
||||||
loop = events.get_running_loop()
|
loop = events.get_running_loop()
|
||||||
future = loop.create_future()
|
future = loop.create_future()
|
||||||
h = loop.call_later(delay,
|
h = loop.call_later(delay,
|
||||||
|
|
|
@ -1609,6 +1609,21 @@ class BaseTaskTests:
|
||||||
self.assertEqual(t.result(), 'yeah')
|
self.assertEqual(t.result(), 'yeah')
|
||||||
self.assertAlmostEqual(0.1, loop.time())
|
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 test_sleep_cancel(self):
|
||||||
|
|
||||||
def gen():
|
def gen():
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Raise :exc:`ValueError` if the ``delay`` argument to :func:`asyncio.sleep` is a NaN (matching :func:`time.sleep`).
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue