Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
changed max sleep for not unix systems
  • Loading branch information
DominicPrince2003 authored Jul 6, 2025
commit a9f152c2160dc0819e5e3861d3acf2777c284eb6
2 changes: 1 addition & 1 deletion vm/src/stdlib/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mod decl {
} else {
return Err(vm.new_type_error("sleep() argument must be a number"));
};
if !secs.is_finite() || secs < 0.0 || secs > u64::MAX as f64 {
if !secs.is_finite() || secs < 0.0 || secs > (u32::MAX / 1000) as f64 {
return Err(vm.new_value_error("sleep length must be a non-negative finite number"));
}
Comment on lines +94 to +104
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This validation logic is duplicated in the unix implementation of sleep as well (lines 710-720). To improve maintainability and avoid code duplication, consider extracting this logic into a shared helper function. This function could be defined at the module level and be called by both sleep implementations.

For example:

fn get_sleep_secs(secs_obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
    // Try to get as float first, if that fails try as integer
    let secs = if let Ok(float_val) = f64::try_from_object(vm, secs_obj.clone()) {
        float_val
    } else if let Ok(int_val) = i64::try_from_object(vm, secs_obj) {
        int_val as f64
    } else {
        return Err(vm.new_type_error("sleep() argument must be a number"));
    };

    if !secs.is_finite() || secs < 0.0 || secs > u64::MAX as f64 {
        return Err(vm.new_value_error("sleep length must be a non-negative finite number"));
    }
    Ok(secs)
}


Expand Down