-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-105331: Fix asyncio.sleep() bug #105513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -640,9 +640,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, 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)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, seems there need to add check that delay is finite number. Use something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This appears to be more accurate. Thank you! |
||
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 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fix asyncio.sleep(float('nan')) does not raise ValueError bug |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is incorrect because of 32-bit platform (and that's why asyncio test failing on Windows 32-bit).
sys.maxsize
on 32-bit platform equal to2 ** 31 -1
.So, in that case
sys.maxsize / SEC_TO_NS
is equal to2.147483647
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh!
I forgot about this matter.
In
timemodule.c
, INT64_MAX is used and is not subject to the limitation of 32-bit.If I write it as
INT64_MAX = 2**64 - 1
, how would it be?if (delay > INT64_MAX / SEC_TO_NS)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's correct solution, but I'm not expert in this area. Let's wait for other opinions