Skip to content

Commit 468d21e

Browse files
Copilotyouknowone
andauthored
refactor: extract phase 2 and 4 host helpers
Agent-Logs-Url: https://github.com/RustPython/RustPython/sessions/48d1e64d-37ce-409f-b511-8e61a349665c Co-authored-by: youknowone <[email protected]>
1 parent 7852ece commit 468d21e

9 files changed

Lines changed: 115 additions & 80 deletions

File tree

crates/host_env/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ pub mod fileutils;
1414

1515
#[cfg(windows)]
1616
pub mod windows;
17+
18+
#[cfg(unix)]
19+
pub mod posix;
20+
#[cfg(all(unix, not(target_os = "redox"), not(target_os = "android")))]
21+
pub mod shm;
22+
#[cfg(unix)]
23+
pub mod signal;
24+
pub mod time;

crates/host_env/src/posix.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::os::fd::BorrowedFd;
2+
3+
pub fn set_inheritable(fd: BorrowedFd<'_>, inheritable: bool) -> nix::Result<()> {
4+
use nix::fcntl;
5+
6+
let flags = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd, fcntl::FcntlArg::F_GETFD)?);
7+
let mut new_flags = flags;
8+
new_flags.set(fcntl::FdFlag::FD_CLOEXEC, !inheritable);
9+
if flags != new_flags {
10+
fcntl::fcntl(fd, fcntl::FcntlArg::F_SETFD(new_flags))?;
11+
}
12+
Ok(())
13+
}

crates/host_env/src/shm.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use core::ffi::CStr;
2+
use std::io;
3+
4+
pub fn shm_open(name: &CStr, flags: libc::c_int, mode: libc::c_uint) -> io::Result<libc::c_int> {
5+
#[cfg(target_os = "freebsd")]
6+
let mode = mode.try_into().unwrap();
7+
8+
let fd = unsafe { libc::shm_open(name.as_ptr(), flags, mode) };
9+
if fd == -1 {
10+
Err(io::Error::last_os_error())
11+
} else {
12+
Ok(fd)
13+
}
14+
}
15+
16+
pub fn shm_unlink(name: &CStr) -> io::Result<()> {
17+
let ret = unsafe { libc::shm_unlink(name.as_ptr()) };
18+
if ret == -1 {
19+
Err(io::Error::last_os_error())
20+
} else {
21+
Ok(())
22+
}
23+
}

crates/host_env/src/signal.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pub fn timeval_to_double(tv: &libc::timeval) -> f64 {
2+
tv.tv_sec as f64 + (tv.tv_usec as f64 / 1_000_000.0)
3+
}
4+
5+
pub fn double_to_timeval(val: f64) -> libc::timeval {
6+
libc::timeval {
7+
tv_sec: val.trunc() as _,
8+
tv_usec: (val.fract() * 1_000_000.0) as _,
9+
}
10+
}
11+
12+
pub fn itimerval_to_tuple(it: &libc::itimerval) -> (f64, f64) {
13+
(
14+
timeval_to_double(&it.it_value),
15+
timeval_to_double(&it.it_interval),
16+
)
17+
}

crates/host_env/src/time.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use core::time::Duration;
2+
use std::{
3+
io,
4+
time::{SystemTime, UNIX_EPOCH},
5+
};
6+
7+
pub const SEC_TO_MS: i64 = 1000;
8+
pub const MS_TO_US: i64 = 1000;
9+
pub const SEC_TO_US: i64 = SEC_TO_MS * MS_TO_US;
10+
pub const US_TO_NS: i64 = 1000;
11+
pub const MS_TO_NS: i64 = MS_TO_US * US_TO_NS;
12+
pub const SEC_TO_NS: i64 = SEC_TO_MS * MS_TO_NS;
13+
pub const NS_TO_MS: i64 = 1000 * 1000;
14+
pub const NS_TO_US: i64 = 1000;
15+
16+
pub fn duration_since_system_now() -> io::Result<Duration> {
17+
SystemTime::now()
18+
.duration_since(UNIX_EPOCH)
19+
.map_err(io::Error::other)
20+
}
21+
22+
#[cfg(any(unix, windows))]
23+
pub fn asctime_from_tm(tm: &libc::tm) -> String {
24+
const WDAY_NAME: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
25+
const MON_NAME: [&str; 12] = [
26+
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
27+
];
28+
format!(
29+
"{} {}{:>3} {:02}:{:02}:{:02} {}",
30+
WDAY_NAME[tm.tm_wday as usize],
31+
MON_NAME[tm.tm_mon as usize],
32+
tm.tm_mday,
33+
tm.tm_hour,
34+
tm.tm_min,
35+
tm.tm_sec,
36+
tm.tm_year + 1900
37+
)
38+
}

crates/stdlib/src/posixshmem.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod _posixshmem {
99
use crate::vm::{
1010
FromArgs, PyResult, VirtualMachine, builtins::PyUtf8StrRef, convert::IntoPyException,
1111
};
12-
use rustpython_host_env::os::errno_io_error;
12+
use rustpython_host_env::shm;
1313

1414
#[derive(FromArgs)]
1515
struct ShmOpenArgs {
@@ -25,26 +25,12 @@ mod _posixshmem {
2525
fn shm_open(args: ShmOpenArgs, vm: &VirtualMachine) -> PyResult<libc::c_int> {
2626
let name = CString::new(args.name.as_str()).map_err(|e| e.into_pyexception(vm))?;
2727
let mode: libc::c_uint = args.mode as _;
28-
#[cfg(target_os = "freebsd")]
29-
let mode = mode.try_into().unwrap();
30-
// SAFETY: `name` is a NUL-terminated string and `shm_open` does not write through it.
31-
let fd = unsafe { libc::shm_open(name.as_ptr(), args.flags, mode) };
32-
if fd == -1 {
33-
Err(errno_io_error().into_pyexception(vm))
34-
} else {
35-
Ok(fd)
36-
}
28+
shm::shm_open(name.as_c_str(), args.flags, mode).map_err(|e| e.into_pyexception(vm))
3729
}
3830

3931
#[pyfunction]
4032
fn shm_unlink(name: PyUtf8StrRef, vm: &VirtualMachine) -> PyResult<()> {
4133
let name = CString::new(name.as_str()).map_err(|e| e.into_pyexception(vm))?;
42-
// SAFETY: `name` is a valid NUL-terminated string and `shm_unlink` only reads it.
43-
let ret = unsafe { libc::shm_unlink(name.as_ptr()) };
44-
if ret == -1 {
45-
Err(errno_io_error().into_pyexception(vm))
46-
} else {
47-
Ok(())
48-
}
34+
shm::shm_unlink(name.as_c_str()).map_err(|e| e.into_pyexception(vm))
4935
}
5036
}

crates/vm/src/stdlib/_signal.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub(crate) mod _signal {
1313
function::{ArgIntoFloat, OptionalArg},
1414
};
1515
use core::sync::atomic::{self, Ordering};
16+
#[cfg(unix)]
17+
use rustpython_host_env::signal::{double_to_timeval, itimerval_to_tuple};
1618

1719
#[cfg(any(unix, windows))]
1820
use libc::sighandler_t;
@@ -280,27 +282,6 @@ pub(crate) mod _signal {
280282
Ok(())
281283
}
282284

283-
#[cfg(unix)]
284-
fn timeval_to_double(tv: &libc::timeval) -> f64 {
285-
tv.tv_sec as f64 + (tv.tv_usec as f64 / 1_000_000.0)
286-
}
287-
288-
#[cfg(unix)]
289-
fn double_to_timeval(val: f64) -> libc::timeval {
290-
libc::timeval {
291-
tv_sec: val.trunc() as _,
292-
tv_usec: ((val.fract()) * 1_000_000.0) as _,
293-
}
294-
}
295-
296-
#[cfg(unix)]
297-
fn itimerval_to_tuple(it: &libc::itimerval) -> (f64, f64) {
298-
(
299-
timeval_to_double(&it.it_value),
300-
timeval_to_double(&it.it_interval),
301-
)
302-
}
303-
304285
#[cfg(unix)]
305286
#[pyfunction]
306287
fn setitimer(

crates/vm/src/stdlib/posix.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
// spell-checker:disable
22

3-
use std::os::fd::BorrowedFd;
4-
53
pub(crate) use module::module_def;
64

7-
pub fn set_inheritable(fd: BorrowedFd<'_>, inheritable: bool) -> nix::Result<()> {
8-
use nix::fcntl;
9-
let flags = fcntl::FdFlag::from_bits_truncate(fcntl::fcntl(fd, fcntl::FcntlArg::F_GETFD)?);
10-
let mut new_flags = flags;
11-
new_flags.set(fcntl::FdFlag::FD_CLOEXEC, !inheritable);
12-
if flags != new_flags {
13-
fcntl::fcntl(fd, fcntl::FcntlArg::F_SETFD(new_flags))?;
14-
}
15-
Ok(())
16-
}
5+
pub use rustpython_host_env::posix::set_inheritable;
176

187
#[pymodule(name = "posix", with(
198
super::os::_os,

crates/vm/src/stdlib/time.rs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod decl {
4141
naive::{NaiveDate, NaiveDateTime, NaiveTime},
4242
};
4343
use core::time::Duration;
44+
use rustpython_host_env::time::{self as host_time, asctime_from_tm};
4445
#[cfg(target_env = "msvc")]
4546
#[cfg(not(target_arch = "wasm32"))]
4647
use windows_sys::Win32::System::Time::{GetTimeZoneInformation, TIME_ZONE_INFORMATION};
@@ -56,27 +57,24 @@ mod decl {
5657
}
5758

5859
#[allow(dead_code)]
59-
pub(super) const SEC_TO_MS: i64 = 1000;
60+
pub(super) const SEC_TO_MS: i64 = host_time::SEC_TO_MS;
6061
#[allow(dead_code)]
61-
pub(super) const MS_TO_US: i64 = 1000;
62+
pub(super) const MS_TO_US: i64 = host_time::MS_TO_US;
6263
#[allow(dead_code)]
63-
pub(super) const SEC_TO_US: i64 = SEC_TO_MS * MS_TO_US;
64+
pub(super) const SEC_TO_US: i64 = host_time::SEC_TO_US;
6465
#[allow(dead_code)]
65-
pub(super) const US_TO_NS: i64 = 1000;
66+
pub(super) const US_TO_NS: i64 = host_time::US_TO_NS;
6667
#[allow(dead_code)]
67-
pub(super) const MS_TO_NS: i64 = MS_TO_US * US_TO_NS;
68+
pub(super) const MS_TO_NS: i64 = host_time::MS_TO_NS;
6869
#[allow(dead_code)]
69-
pub(super) const SEC_TO_NS: i64 = SEC_TO_MS * MS_TO_NS;
70+
pub(super) const SEC_TO_NS: i64 = host_time::SEC_TO_NS;
7071
#[allow(dead_code)]
71-
pub(super) const NS_TO_MS: i64 = 1000 * 1000;
72+
pub(super) const NS_TO_MS: i64 = host_time::NS_TO_MS;
7273
#[allow(dead_code)]
73-
pub(super) const NS_TO_US: i64 = 1000;
74+
pub(super) const NS_TO_US: i64 = host_time::NS_TO_US;
7475

7576
fn duration_since_system_now(vm: &VirtualMachine) -> PyResult<Duration> {
76-
use std::time::{SystemTime, UNIX_EPOCH};
77-
78-
SystemTime::now()
79-
.duration_since(UNIX_EPOCH)
77+
host_time::duration_since_system_now()
8078
.map_err(|e| vm.new_value_error(format!("Time error: {e:?}")))
8179
}
8280

@@ -486,24 +484,6 @@ mod decl {
486484
}
487485
}
488486

489-
#[cfg(any(unix, windows))]
490-
fn asctime_from_tm(tm: &libc::tm) -> String {
491-
const WDAY_NAME: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
492-
const MON_NAME: [&str; 12] = [
493-
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
494-
];
495-
format!(
496-
"{} {}{:>3} {:02}:{:02}:{:02} {}",
497-
WDAY_NAME[tm.tm_wday as usize],
498-
MON_NAME[tm.tm_mon as usize],
499-
tm.tm_mday,
500-
tm.tm_hour,
501-
tm.tm_min,
502-
tm.tm_sec,
503-
tm.tm_year + 1900
504-
)
505-
}
506-
507487
#[cfg(not(any(unix, windows)))]
508488
impl OptionalArg<StructTimeData> {
509489
fn naive_or_local(self, vm: &VirtualMachine) -> PyResult<NaiveDateTime> {

0 commit comments

Comments
 (0)