-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Introduce TimeoutSeconds utility type for timeout parameters #7271
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
Open
ever0de
wants to merge
3
commits into
RustPython:main
Choose a base branch
from
ever0de:refactor/introduce-timeout-seconds-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+63
−26
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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,34 @@ | ||
| use crate::{PyObjectRef, PyResult, TryFromObject, VirtualMachine}; | ||
|
|
||
| /// A Python timeout value that accepts both `float` and `int`. | ||
| /// | ||
| /// `TimeoutSeconds` implements `FromArgs` so that a built-in function can accept | ||
| /// timeout parameters given as either `float` or `int`, normalizing them to `f64`. | ||
| #[derive(Debug, Clone, Copy, PartialEq)] | ||
| pub struct TimeoutSeconds { | ||
| value: f64, | ||
| } | ||
|
|
||
| impl TimeoutSeconds { | ||
| pub const fn new(secs: f64) -> Self { | ||
| Self { value: secs } | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn to_secs_f64(self) -> f64 { | ||
| self.value | ||
| } | ||
| } | ||
|
|
||
| impl TryFromObject for TimeoutSeconds { | ||
| fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> { | ||
| let value = match super::Either::<f64, i64>::try_from_object(vm, obj)? { | ||
| super::Either::A(f) => f, | ||
| super::Either::B(i) => i as f64, | ||
| }; | ||
| if value.is_nan() { | ||
| return Err(vm.new_value_error("Invalid value NaN (not a number)".to_owned())); | ||
| } | ||
| Ok(Self { value }) | ||
| } | ||
| } |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
NaN handling is inconsistent with
acquire_lock_impl!.The condition
secs >= 0.0(line 1099) evaluates tofalsefor NaN, causing NaN inputs to silently become infinite waits (via theelsebranch). In contrast,acquire_lock_impl!(lines 84–92) routes NaN through the validation path where!nanos.is_finite()correctly raises anOverflowError.To match
acquire_lock_impl!semantics and the intent of the previous fix, invert the condition so NaN reaches the validation logic:Proposed fix
Some(t) => { let secs = t.to_secs_f64(); - if secs >= 0.0 { + if secs < 0.0 { + None + } else { let nanos = secs * 1_000_000_000.0; if secs > TIMEOUT_MAX || nanos < 0.0 || !nanos.is_finite() { return Err(vm.new_overflow_error( "timestamp too large to convert to Rust Duration".to_owned(), )); } Some(Duration::from_secs_f64(secs)) - } else { - None } }📝 Committable suggestion
🤖 Prompt for AI Agents
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.
This is already addressed —
TimeoutSeconds::try_from_object()rejects NaN withValueError("Invalid value NaN (not a number)")beforejoin()is ever called, so NaN cannot reach this code path.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.
🧩 Analysis chain
🏁 Script executed:
Length of output: 460
@ever0de, you're absolutely right, and I apologize for the incorrect review comment.The
TimeoutSeconds::try_from_object()implementation explicitly validates and rejects NaN values:This means NaN is caught during the conversion from Python object to
TimeoutSeconds, so by the timejoin()receives aTimeoutSecondsparameter, it's guaranteed not to be NaN. The current implementation is correct as-is.🧠 Learnings used