-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Handle negative time.sleep values #5906
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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,8 +90,25 @@ mod decl { | |
|
||
#[cfg(not(unix))] | ||
#[pyfunction] | ||
fn sleep(dur: Duration) { | ||
fn sleep(seconds: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { | ||
let dur = seconds.try_into_value::<Duration>(vm).map_err(|e| { | ||
if e.class().is(vm.ctx.exceptions.value_error) { | ||
// Check if this is a "negative duration" error by examining the args | ||
if let Some(args) = e.args().first() { | ||
if let Ok(s) = args.str(vm) { | ||
if s.as_str() == "negative duration" { | ||
return vm.new_value_error("sleep length must be non-negative"); | ||
} | ||
} | ||
} | ||
e | ||
} else { | ||
e | ||
} | ||
})?; | ||
|
||
std::thread::sleep(dur); | ||
Ok(()) | ||
} | ||
|
||
#[cfg(not(target_os = "wasi"))] | ||
|
@@ -525,7 +542,7 @@ mod platform { | |
use super::decl::{SEC_TO_NS, US_TO_NS}; | ||
#[cfg_attr(target_os = "macos", allow(unused_imports))] | ||
use crate::{ | ||
PyObject, PyRef, PyResult, TryFromBorrowedObject, VirtualMachine, | ||
AsObject, PyObject, PyObjectRef, PyRef, PyResult, TryFromBorrowedObject, VirtualMachine, | ||
builtins::{PyNamespace, PyStrRef}, | ||
convert::IntoPyException, | ||
}; | ||
|
@@ -691,8 +708,22 @@ mod platform { | |
} | ||
|
||
#[pyfunction] | ||
fn sleep(dur: Duration, vm: &VirtualMachine) -> PyResult<()> { | ||
// this is basically std::thread::sleep, but that catches interrupts and we don't want to; | ||
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 comment is not expected to be removed |
||
fn sleep(seconds: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { | ||
let dur = seconds.try_into_value::<Duration>(vm).map_err(|e| { | ||
if e.class().is(vm.ctx.exceptions.value_error) { | ||
// Check if this is a "negative duration" error by examining the args | ||
if let Some(args) = e.args().first() { | ||
if let Ok(s) = args.str(vm) { | ||
if s.as_str() == "negative duration" { | ||
return vm.new_value_error("sleep length must be non-negative"); | ||
} | ||
} | ||
} | ||
e | ||
} else { | ||
e | ||
} | ||
})?; | ||
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 duplicates the error mapping logic in the let dur = seconds.try_into_value::<Duration>(vm).map_err(|e| {
if e.class().is(vm.ctx.exceptions.value_error) {
if let Some(s) = e.args().first().and_then(|arg| arg.str(vm).ok()) {
if s.as_str() == "negative duration" {
return vm.new_value_error("sleep length must be non-negative");
}
}
}
e
})?; |
||
|
||
let ts = TimeSpec::from(dur); | ||
let res = unsafe { libc::nanosleep(ts.as_ref(), std::ptr::null_mut()) }; | ||
|
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 error mapping logic is verbose and duplicated in the
unix
implementation ofsleep
(lines 712-726). Relying on string matching for error messages can be fragile. Consider extracting this logic into a helper function to avoid duplication.