From 5c7843214d2ec5232ec9ca59336b8cdc19ff0460 Mon Sep 17 00:00:00 2001 From: weijay Date: Thu, 8 Jun 2023 22:03:32 +0800 Subject: [PATCH 1/2] gh-105331: Fix asyncio.sleep() bug --- Lib/asyncio/tasks.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 8d5bde09ea9b5b..b5cddb086801cc 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -639,10 +639,34 @@ def __sleep0(): """ yield +def __check_delay(delay): + """Check if the value of 'delay' is valid.""" + + import sys + import math + + SEC_TO_NS = 1000 * 1000 * 1000 + + if not (isinstance(delay, int) or isinstance(delay, float)): + raise TypeError(f"'{type(delay)} object cannot be interpreted as an integer'") + + # According to Modules/timemodule.c + if (delay > sys.maxsize / SEC_TO_NS): + raise OverflowError("timestamp too large to convert to C _PyTime_t") + + if (delay < 0): + raise ValueError("sleep length must be non-negative") + + if (math.isnan(delay)): + raise ValueError("Invalid value NaN (not a number)") + async def sleep(delay, result=None): """Coroutine that completes after a given time (in seconds).""" - if delay <= 0: + + __check_delay(delay) + + if delay == 0: await __sleep0() return result From b6affbdee0e6b4a621193d57c5824e775cf5e162 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 8 Jun 2023 14:21:48 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-06-08-14-21-47.gh-issue-105501.LqzG0A.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-06-08-14-21-47.gh-issue-105501.LqzG0A.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-08-14-21-47.gh-issue-105501.LqzG0A.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-08-14-21-47.gh-issue-105501.LqzG0A.rst new file mode 100644 index 00000000000000..2dac1601832376 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-08-14-21-47.gh-issue-105501.LqzG0A.rst @@ -0,0 +1 @@ +Fix asyncio.sleep(float('nan')) does not raise ValueError