Skip to content

[5895] added code to raise error in sleep if invalid value #5907

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,17 @@ def test_conversions(self):
def test_sleep(self):
self.assertRaises(ValueError, time.sleep, -2)
self.assertRaises(ValueError, time.sleep, -1)
self.assertRaises(ValueError, time.sleep, float('nan'))
self.assertRaises(ValueError, time.sleep, float('inf'))
self.assertRaises(ValueError, time.sleep, -float('inf'))
self.assertRaises(ValueError, time.sleep, 1e100)
time.sleep(0)
time.sleep(0.000001)
time.sleep(1e-9)
time.sleep(0.5)
time.sleep(1.2)
time.sleep(2)
Comment on lines +158 to +167
Copy link
Member

Choose a reason for hiding this comment

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

Since Lib/test/test_time.py is originated from CPython, we usually don't directly edit this file.

We usually add our original test under extra_tests like extra_tests/snippets/stdlib_time.py.

If you also want to contribute to CPython, you can submit a similar patch to CPyhton project: https://github.com/python/cpython



# TODO: RUSTPYTHON
@unittest.expectedFailure
Expand Down
35 changes: 30 additions & 5 deletions vm/src/stdlib/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,22 @@ mod decl {

#[cfg(not(unix))]
#[pyfunction]
fn sleep(dur: Duration) {
std::thread::sleep(dur);
fn sleep(secs: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
// 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.clone()) {
float_val
} else if let Ok(int_val) = i64::try_from_object(vm, secs) {
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 > (u32::MAX / 1000) as f64 {
return Err(vm.new_value_error("sleep length must be a non-negative finite number"));
}

let duration = Duration::from_secs_f64(secs);
std::thread::sleep(duration);
Ok(())
}

#[cfg(not(target_os = "wasi"))]
Expand Down Expand Up @@ -527,7 +541,7 @@ mod platform {
use crate::{
PyObject, PyRef, PyResult, TryFromBorrowedObject, VirtualMachine,
builtins::{PyNamespace, PyStrRef},
convert::IntoPyException,
convert::{IntoPyException,TryFromObject},
};
use nix::{sys::time::TimeSpec, time::ClockId};
use std::time::Duration;
Expand Down Expand Up @@ -691,9 +705,20 @@ mod platform {
}

#[pyfunction]
fn sleep(dur: Duration, vm: &VirtualMachine) -> PyResult<()> {
fn sleep(secs: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
// this is basically std::thread::sleep, but that catches interrupts and we don't want to;

// 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.clone()) {
float_val
} else if let Ok(int_val) = i64::try_from_object(vm, secs) {
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"));
}
let dur = Duration::from_secs_f64(secs);
let ts = TimeSpec::from(dur);
let res = unsafe { libc::nanosleep(ts.as_ref(), std::ptr::null_mut()) };
let interrupted = res == -1 && nix::Error::last_raw() == libc::EINTR;
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy