Skip to content
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

gh-105331: Fix asyncio.sleep() bug #105641

Merged
merged 14 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import functools
import inspect
import itertools
import math
import types
import warnings
import weakref
Expand Down Expand Up @@ -642,6 +643,11 @@ 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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this check after delay <= 0 check to not slow down the common case of sleep(0).

raise ValueError("Invalid delay: NaN (not a number)")

if delay <= 0:
await __sleep0()
return result
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``asyncio.sleep()`` error handel. When call ``asyncio.sleep(float('nan'))``, it should be raise ValueError.
weijay0804 marked this conversation as resolved.
Show resolved Hide resolved