Skip to content

Commit 02c4357

Browse files
committed
Implement os.getpriority and os.setpriority
1 parent 6e7de79 commit 02c4357

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

vm/src/stdlib/os.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl IntoPyException for &'_ io::Error {
187187
| Some(errors::EALREADY)
188188
| Some(errors::EWOULDBLOCK)
189189
| Some(errors::EINPROGRESS) => vm.ctx.exceptions.blocking_io_error.clone(),
190+
Some(errors::ESRCH) => vm.ctx.exceptions.process_lookup_error.clone(),
190191
_ => vm.ctx.exceptions.os_error.clone(),
191192
},
192193
};
@@ -308,6 +309,9 @@ mod _os {
308309
O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY, SEEK_CUR, SEEK_END,
309310
SEEK_SET,
310311
};
312+
#[cfg(not(target_os = "windows"))]
313+
#[pyattr]
314+
use libc::{PRIO_PGRP, PRIO_PROCESS, PRIO_USER};
311315
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))]
312316
#[pyattr]
313317
use libc::{SEEK_DATA, SEEK_HOLE};
@@ -1358,7 +1362,7 @@ mod posix {
13581362
use crate::builtins::list::PyListRef;
13591363
use crate::pyobject::PyIterable;
13601364
use bitflags::bitflags;
1361-
use nix::errno::Errno;
1365+
use nix::errno::{errno, Errno};
13621366
use nix::unistd::{self, Gid, Pid, Uid};
13631367
use std::convert::TryFrom;
13641368
pub(super) use std::os::unix::fs::OpenOptionsExt;
@@ -2521,6 +2525,42 @@ mod posix {
25212525
.collect(),
25222526
))
25232527
}
2528+
2529+
cfg_if::cfg_if! {
2530+
if #[cfg(all(target_os = "linux", target_env = "gnu"))] {
2531+
type PriorityWhichType = libc::__priority_which_t;
2532+
} else {
2533+
type PriorityWhichType = libc::c_int;
2534+
}
2535+
}
2536+
2537+
#[cfg(not(target_os = "windows"))]
2538+
#[pyfunction]
2539+
fn getpriority(which: PriorityWhichType, who: u32, vm: &VirtualMachine) -> PyResult {
2540+
Errno::clear();
2541+
let retval = unsafe { libc::getpriority(which, who) };
2542+
if errno() != 0 {
2543+
Err(errno_err(vm))
2544+
} else {
2545+
Ok(vm.ctx.new_int(retval))
2546+
}
2547+
}
2548+
2549+
#[cfg(not(target_os = "windows"))]
2550+
#[pyfunction]
2551+
fn setpriority(
2552+
which: PriorityWhichType,
2553+
who: u32,
2554+
priority: i32,
2555+
vm: &VirtualMachine,
2556+
) -> PyResult<()> {
2557+
let retval = unsafe { libc::setpriority(which, who, priority) };
2558+
if retval == -1 {
2559+
Err(errno_err(vm))
2560+
} else {
2561+
Ok(())
2562+
}
2563+
}
25242564
}
25252565
#[cfg(unix)]
25262566
use posix as platform;

0 commit comments

Comments
 (0)