Skip to content

Commit ad82f59

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

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

vm/src/stdlib/os.rs

Lines changed: 51 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,52 @@ mod posix {
25212525
.collect(),
25222526
))
25232527
}
2528+
2529+
#[cfg(target_env = "gnu")]
2530+
#[pyfunction]
2531+
fn getpriority(which: u32, who: u32, vm: &VirtualMachine) -> PyResult {
2532+
Errno::clear();
2533+
let retval = unsafe { libc::getpriority(which, who) };
2534+
if errno() != 0 {
2535+
Err(errno_err(vm))
2536+
} else {
2537+
Ok(vm.ctx.new_int(retval))
2538+
}
2539+
}
2540+
2541+
#[cfg(target_env = "gnu")]
2542+
#[pyfunction]
2543+
fn setpriority(which: u32, who: u32, priority: i32, vm: &VirtualMachine) -> PyResult<()> {
2544+
let retval = unsafe { libc::setpriority(which, who, priority) };
2545+
if retval == -1 {
2546+
Err(errno_err(vm))
2547+
} else {
2548+
Ok(())
2549+
}
2550+
}
2551+
2552+
#[cfg(all(not(target_env = "gnu"), not(target_os = "windows")))]
2553+
#[pyfunction]
2554+
fn getpriority(which: i32, who: u32, vm: &VirtualMachine) -> PyResult {
2555+
Errno::clear();
2556+
let retval = unsafe { libc::getpriority(which, who) };
2557+
if errno() != 0 {
2558+
Err(errno_err(vm))
2559+
} else {
2560+
Ok(vm.ctx.new_int(retval))
2561+
}
2562+
}
2563+
2564+
#[cfg(all(not(target_env = "gnu"), not(target_os = "windows")))]
2565+
#[pyfunction]
2566+
fn setpriority(which: i32, who: u32, priority: i32, vm: &VirtualMachine) -> PyResult<()> {
2567+
let retval = unsafe { libc::setpriority(which, who, priority) };
2568+
if retval == -1 {
2569+
Err(errno_err(vm))
2570+
} else {
2571+
Ok(())
2572+
}
2573+
}
25242574
}
25252575
#[cfg(unix)]
25262576
use posix as platform;

0 commit comments

Comments
 (0)