Skip to content

Commit 0a8df91

Browse files
authored
Merge pull request RustPython#2048 from BasixKOR/os-waitpid-windows
Implement os.waitpid on Windows
2 parents 55e97af + cf9370a commit 0a8df91

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

vm/src/stdlib/os.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,6 @@ mod posix {
19711971
unsafe { libc::WEXITSTATUS(status) }
19721972
}
19731973

1974-
// TODO: os.wait[pid] for windows
19751974
#[pyfunction]
19761975
fn waitpid(pid: libc::pid_t, opt: i32, vm: &VirtualMachine) -> PyResult<(libc::pid_t, i32)> {
19771976
let mut status = 0;
@@ -2134,6 +2133,8 @@ mod nt {
21342133
use super::*;
21352134
pub(super) use std::os::windows::fs::OpenOptionsExt;
21362135
use std::os::windows::io::RawHandle;
2136+
#[cfg(target_env = "msvc")]
2137+
use winapi::vc::vcruntime::intptr_t;
21372138

21382139
pub(super) type OpenFlags = u32;
21392140

@@ -2290,6 +2291,46 @@ mod nt {
22902291
get_stats().map_err(|e| convert_io_error(vm, e))
22912292
}
22922293

2294+
// cwait is available on MSVC only (according to CPython)
2295+
#[cfg(target_env = "msvc")]
2296+
extern "C" {
2297+
fn _cwait(termstat: *mut i32, procHandle: intptr_t, action: i32) -> intptr_t;
2298+
fn _get_errno(pValue: *mut i32) -> i32;
2299+
}
2300+
2301+
#[cfg(target_env = "msvc")]
2302+
#[pyfunction]
2303+
fn waitpid(pid: intptr_t, opt: i32, vm: &VirtualMachine) -> PyResult<(intptr_t, i32)> {
2304+
const ECHILD: i32 = 10;
2305+
const EINVAL: i32 = 22;
2306+
2307+
let mut status = 0;
2308+
let pid = unsafe { suppress_iph!(_cwait(&mut status, pid, opt)) };
2309+
if pid == -1 {
2310+
let mut errno = 0;
2311+
unsafe { _get_errno(&mut errno) };
2312+
match errno {
2313+
ECHILD => Err(vm.new_exception_msg(
2314+
vm.ctx.exceptions.os_error.clone(),
2315+
"ECHILD: No spawned processes".to_owned(),
2316+
)),
2317+
EINVAL => Err(vm.new_exception_msg(
2318+
vm.ctx.exceptions.os_error.clone(),
2319+
"EINVAL: Invalid argument".to_owned(),
2320+
)),
2321+
_ => unreachable!(),
2322+
}
2323+
} else {
2324+
Ok((pid, status << 8))
2325+
}
2326+
}
2327+
2328+
#[cfg(target_env = "msvc")]
2329+
#[pyfunction]
2330+
fn wait(vm: &VirtualMachine) -> PyResult<(intptr_t, i32)> {
2331+
waitpid(-1, 0, vm)
2332+
}
2333+
22932334
#[pyfunction]
22942335
fn kill(pid: i32, sig: isize, vm: &VirtualMachine) -> PyResult<()> {
22952336
{

0 commit comments

Comments
 (0)