-
-
Notifications
You must be signed in to change notification settings - Fork 393
timeout functions now raise ValueError on NaN inputs #2667
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Timeout functions now raise `ValueError` if passed `math.nan`. This includes `trio.sleep`, `trio.sleep_until`, `trio.move_on_at`, `trio.move_on_after`, `trio.fail_at` and `trio.fail_after`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import math | ||
| from contextlib import contextmanager | ||
|
|
||
| import trio | ||
|
|
@@ -10,7 +11,12 @@ def move_on_at(deadline): | |
| Args: | ||
| deadline (float): The deadline. | ||
|
|
||
| Raises: | ||
| ValueError: if deadline is NaN. | ||
|
|
||
| """ | ||
| if math.isnan(deadline): | ||
| raise ValueError("deadline must not be NaN") | ||
| return trio.CancelScope(deadline=deadline) | ||
|
|
||
|
|
||
|
|
@@ -22,10 +28,9 @@ def move_on_after(seconds): | |
| seconds (float): The timeout. | ||
|
|
||
| Raises: | ||
| ValueError: if timeout is less than zero. | ||
| ValueError: if timeout is less than zero or NaN. | ||
|
|
||
| """ | ||
|
|
||
| if seconds < 0: | ||
| raise ValueError("timeout must be non-negative") | ||
| return move_on_at(trio.current_time() + seconds) | ||
|
|
@@ -52,6 +57,9 @@ async def sleep_until(deadline): | |
| the past, in which case this function executes a checkpoint but | ||
| does not block. | ||
|
|
||
| Raises: | ||
| ValueError: if deadline is NaN. | ||
|
|
||
| """ | ||
| with move_on_at(deadline): | ||
| await sleep_forever() | ||
|
|
@@ -65,7 +73,7 @@ async def sleep(seconds): | |
| insert a checkpoint without actually blocking. | ||
|
|
||
| Raises: | ||
| ValueError: if *seconds* is negative. | ||
| ValueError: if *seconds* is negative or NaN. | ||
|
|
||
| """ | ||
| if seconds < 0: | ||
|
|
@@ -96,9 +104,13 @@ def fail_at(deadline): | |
| :func:`fail_at`, then it's caught and :exc:`TooSlowError` is raised in its | ||
| place. | ||
|
|
||
| Args: | ||
| deadline (float): The deadline. | ||
|
|
||
| Raises: | ||
| TooSlowError: if a :exc:`Cancelled` exception is raised in this scope | ||
| and caught by the context manager. | ||
| ValueError: if deadline is NaN. | ||
|
|
||
| """ | ||
|
|
||
|
|
@@ -119,10 +131,13 @@ def fail_after(seconds): | |
| it's caught and discarded. When it reaches :func:`fail_after`, then it's | ||
| caught and :exc:`TooSlowError` is raised in its place. | ||
|
|
||
| Args: | ||
| seconds (float): The timeout. | ||
|
|
||
|
Comment on lines
+134
to
+136
Contributor
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. Just a comment that I really don't like how this is so easy to forget :S
Member
Author
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 sounds like something that can be auto-generated / have the signature displayed in another manner in the doc, once the public interface is fully typed. |
||
| Raises: | ||
| TooSlowError: if a :exc:`Cancelled` exception is raised in this scope | ||
| and caught by the context manager. | ||
| ValueError: if *seconds* is less than zero. | ||
| ValueError: if *seconds* is less than zero or NaN. | ||
|
|
||
| """ | ||
| if seconds < 0: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.