FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-105331: Fix asyncio.sleep() bug by weijay0804 · Pull Request #105501 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 25 additions & 1 deletion Lib/asyncio/tasks.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -639,10 +639,34 @@ def __sleep0():
"""
yield

def __check_delay(delay):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Nitpick: Does it really need double underscore? If you want to mark something as "private API" use one leading underscore

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Get it!
Thank you

"""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)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
if not (isinstance(delay, int) or isinstance(delay, float)):
if not isinstance(delay, (int, float)):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Wow! This is much clearer now.

raise TypeError(f"'{type(delay)} object cannot be interpreted as an integer'")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
raise TypeError(f"'{type(delay)} object cannot be interpreted as an integer'")
raise TypeError(f"{type(delay)} object cannot be interpreted as an integer")

There's no need for single quotes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ok
Thank you


# 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

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix asyncio.sleep(float('nan')) does not raise ValueError

Back | FazBrowse Home | New Git URL