From f2008da416ce82618b58ee72b42f405722e62be9 Mon Sep 17 00:00:00 2001 From: weijay Date: Sun, 11 Jun 2023 17:07:21 +0800 Subject: [PATCH 01/14] gh-105331: Fix asyncio.sleep() bug --- Lib/asyncio/tasks.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 8d5bde09ea9b5b..3d25e306369580 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -642,6 +642,12 @@ def __sleep0(): async def sleep(delay, result=None): """Coroutine that completes after a given time (in seconds).""" + + # check delay value is not nan + import math + if math.isnan(delay): + raise ValueError("Invalid value NaN (not a number)") + if delay <= 0: await __sleep0() return result From cb1492fb1271b0484ba0eabcda8b97c23bf2a9e8 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 11 Jun 2023 09:14:31 +0000 Subject: [PATCH 02/14] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst new file mode 100644 index 00000000000000..5192c270b7cb8f --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst @@ -0,0 +1 @@ +fix asyncio.sleep(float('nan')) does not raise ValueErro problem From ca806d984361fe388d47ebaf00ce051b78071c4f Mon Sep 17 00:00:00 2001 From: weijay Date: Sun, 11 Jun 2023 18:09:50 +0800 Subject: [PATCH 03/14] run make patchcheck to fix Azure Pipelines PR problem --- Lib/asyncio/tasks.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 3d25e306369580..b8fcd29ad5caaa 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -950,21 +950,21 @@ def callback(): def create_eager_task_factory(custom_task_constructor): """Create a function suitable for use as a task factory on an event-loop. - Example usage: + Example usage: - loop.set_task_factory( - asyncio.create_eager_task_factory(my_task_constructor)) + loop.set_task_factory( + asyncio.create_eager_task_factory(my_task_constructor)) - Now, tasks created will be started immediately (rather than being first - scheduled to an event loop). The constructor argument can be any callable - that returns a Task-compatible object and has a signature compatible - with `Task.__init__`; it must have the `eager_start` keyword argument. + Now, tasks created will be started immediately (rather than being first + scheduled to an event loop). The constructor argument can be any callable + that returns a Task-compatible object and has a signature compatible + with `Task.__init__`; it must have the `eager_start` keyword argument. - Most applications will use `Task` for `custom_task_constructor` and in + Most applications will use `Task` for `custom_task_constructor` and in this case there's no need to call `create_eager_task_factory()` directly. Instead the global `eager_task_factory` instance can be used. E.g. `loop.set_task_factory(asyncio.eager_task_factory)`. - """ + """ def factory(loop, coro, *, name=None, context=None): return custom_task_constructor( From 588bbbd8fa0006c060bece8468366d4e44782bf1 Mon Sep 17 00:00:00 2001 From: weijay Date: Mon, 12 Jun 2023 00:45:39 +0800 Subject: [PATCH 04/14] gh-105331: move import math to the toplevel --- Lib/asyncio/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index b8fcd29ad5caaa..00c3f8582b42cb 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -15,6 +15,7 @@ import functools import inspect import itertools +import math import types import warnings import weakref @@ -644,7 +645,6 @@ async def sleep(delay, result=None): """Coroutine that completes after a given time (in seconds).""" # check delay value is not nan - import math if math.isnan(delay): raise ValueError("Invalid value NaN (not a number)") From 1c178868b8aeab22f18dcf1529bec1d63c8661ae Mon Sep 17 00:00:00 2001 From: weijay Date: Mon, 12 Jun 2023 01:03:57 +0800 Subject: [PATCH 05/14] gh-105331: Improve the content description of the error message --- Lib/asyncio/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 00c3f8582b42cb..c5d998ce9de354 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -646,7 +646,7 @@ async def sleep(delay, result=None): # check delay value is not nan if math.isnan(delay): - raise ValueError("Invalid value NaN (not a number)") + raise ValueError("Invalid delay: NaN (not a number)") if delay <= 0: await __sleep0() From 9bd7ced8253218635f0451620a3d395ddb7d5f7f Mon Sep 17 00:00:00 2001 From: weijay Date: Mon, 12 Jun 2023 01:30:52 +0800 Subject: [PATCH 06/14] gh-105331: Improve description to more detailed --- .../2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst index 5192c270b7cb8f..8ab423901600a9 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst @@ -1 +1 @@ -fix asyncio.sleep(float('nan')) does not raise ValueErro problem +Add ``asyncio.sleep()`` error handel. When call ``asyncio.sleep(float('nan'))``, it should be raise ValueError. From cc03137c460f117950600797ac69c6af182adfa1 Mon Sep 17 00:00:00 2001 From: weijay Date: Mon, 12 Jun 2023 17:29:28 +0800 Subject: [PATCH 07/14] gh-105331: Fix the indentation change --- Lib/asyncio/tasks.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index c5d998ce9de354..bfe82fa3d8c10d 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -950,21 +950,21 @@ def callback(): def create_eager_task_factory(custom_task_constructor): """Create a function suitable for use as a task factory on an event-loop. - Example usage: + Example usage: - loop.set_task_factory( - asyncio.create_eager_task_factory(my_task_constructor)) + loop.set_task_factory( + asyncio.create_eager_task_factory(my_task_constructor)) - Now, tasks created will be started immediately (rather than being first - scheduled to an event loop). The constructor argument can be any callable - that returns a Task-compatible object and has a signature compatible - with `Task.__init__`; it must have the `eager_start` keyword argument. + Now, tasks created will be started immediately (rather than being first + scheduled to an event loop). The constructor argument can be any callable + that returns a Task-compatible object and has a signature compatible + with `Task.__init__`; it must have the `eager_start` keyword argument. - Most applications will use `Task` for `custom_task_constructor` and in + Most applications will use `Task` for `custom_task_constructor` and in this case there's no need to call `create_eager_task_factory()` directly. Instead the global `eager_task_factory` instance can be used. E.g. `loop.set_task_factory(asyncio.eager_task_factory)`. - """ + """ def factory(loop, coro, *, name=None, context=None): return custom_task_constructor( From 4a08061f6b557a8a3ca81e9c20a5c4469544faee Mon Sep 17 00:00:00 2001 From: weijay Date: Mon, 12 Jun 2023 22:21:52 +0800 Subject: [PATCH 08/14] gh-105331: Add test for asyncio.sleep() delay is nan --- Lib/test/test_asyncio/test_tasks.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 6e8a51ce2555d5..4dfaff847edb90 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -1609,6 +1609,21 @@ async def sleeper(dt, arg): 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(): From 11b9d7e480547af8900f6728ab596135c997c24d Mon Sep 17 00:00:00 2001 From: weijay Date: Mon, 12 Jun 2023 22:43:15 +0800 Subject: [PATCH 09/14] gh-105331: Move check delay is nan section after delay <= 0 section --- Lib/asyncio/tasks.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index bfe82fa3d8c10d..fc654883e01ca9 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -644,14 +644,14 @@ def __sleep0(): async def sleep(delay, result=None): """Coroutine that completes after a given time (in seconds).""" - # check delay value is not nan - if math.isnan(delay): - raise ValueError("Invalid delay: NaN (not a number)") - if delay <= 0: await __sleep0() return result + # check delay value is not nan + 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, From 7c3f3eb828647d5ad63946ada67c013ae5beeba4 Mon Sep 17 00:00:00 2001 From: weijay Date: Mon, 12 Jun 2023 22:55:51 +0800 Subject: [PATCH 10/14] gh-105331: Add description for versionchanged --- Doc/library/asyncio-task.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index fe8d028150403d..0d575c2bf82783 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -426,6 +426,8 @@ Sleeping .. versionchanged:: 3.10 Removed the *loop* parameter. + .. versionchanged:: 3.13 + If the *delay* value is nan will raise ValueError. Running Tasks Concurrently ========================== From 730ee4e2254463ae4edd2f737a052876b189d808 Mon Sep 17 00:00:00 2001 From: Jay <74105438+weijay0804@users.noreply.github.com> Date: Mon, 12 Jun 2023 23:35:33 +0800 Subject: [PATCH 11/14] Update Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst I think this way is more clearer. Thank you! Co-authored-by: Guido van Rossum --- .../2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst index 8ab423901600a9..0d6283b04d87b0 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst @@ -1 +1 @@ -Add ``asyncio.sleep()`` error handel. When call ``asyncio.sleep(float('nan'))``, it should be raise ValueError. +Raise :exc:`ValueError` if the `delay` argument to :func:`asyncio.sleep` is a NaN (matching :func:`time.sleep`). From 49c55c69a14375e4e90ee44933dad4b2bb220dd9 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Tue, 13 Jun 2023 01:53:00 +0530 Subject: [PATCH 12/14] minor tweaks --- Doc/library/asyncio-task.rst | 3 ++- Lib/asyncio/tasks.py | 2 -- .../2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst | 3 ++- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index 0d575c2bf82783..6f9a44728ab7ab 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -427,7 +427,8 @@ Sleeping Removed the *loop* parameter. .. versionchanged:: 3.13 - If the *delay* value is nan will raise ValueError. + Raises :exc:`ValueError` if *delay* is :data:`math.nan`. + Running Tasks Concurrently ========================== diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index fc654883e01ca9..4250bb0753879a 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -643,12 +643,10 @@ def __sleep0(): async def sleep(delay, result=None): """Coroutine that completes after a given time (in seconds).""" - if delay <= 0: await __sleep0() return result - # check delay value is not nan if math.isnan(delay): raise ValueError("Invalid delay: NaN (not a number)") diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst index 0d6283b04d87b0..553939acb23d0c 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst @@ -1 +1,2 @@ -Raise :exc:`ValueError` if the `delay` argument to :func:`asyncio.sleep` is a NaN (matching :func:`time.sleep`). +Raise :exc:`ValueError` if the `delay` argument to :func:`asyncio.sleep` is a NaN (matching :func:`time.sleep`). + From c1712ad15d329216b7c9355fee24ad26cfcbfa65 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Tue, 13 Jun 2023 01:54:34 +0530 Subject: [PATCH 13/14] refer to `nan` not `math.nan` --- Doc/library/asyncio-task.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index 6f9a44728ab7ab..3618bcb6d7c6b5 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -427,7 +427,7 @@ Sleeping Removed the *loop* parameter. .. versionchanged:: 3.13 - Raises :exc:`ValueError` if *delay* is :data:`math.nan`. + Raises :exc:`ValueError` if *delay* is :data:`~math.nan`. Running Tasks Concurrently From 5c840c4e5d2af6bd0027f42565265c25d73f2c13 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Tue, 13 Jun 2023 01:58:04 +0530 Subject: [PATCH 14/14] fix news --- .../2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst index 553939acb23d0c..4a3fee0dd64ae0 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-11-09-14-30.gh-issue-105331.nlZvoW.rst @@ -1,2 +1,2 @@ -Raise :exc:`ValueError` if the `delay` argument to :func:`asyncio.sleep` is a NaN (matching :func:`time.sleep`). +Raise :exc:`ValueError` if the ``delay`` argument to :func:`asyncio.sleep` is a NaN (matching :func:`time.sleep`).