Skip to content

Commit fe45a7a

Browse files
authored
host_env: export ctypes, chflags, SIG_DFL (#8766)
* host_env: export ctypes, chflags, SIG_DFL Publish ctypes FUNCFLAG/TYPEFLAG/DICTFLAG and CTYPES_MAX_ARGCOUNT, BSD chflags bits libc does not bind, AT_FDCWD, and SIG_DFL/IGN/ERR. Assisted-by: Claude * stdlib: take ctypes, chflags, SIG_DFL from host_env _ctypes, _stat, os.AT_FDCWD, and Windows SIG_DFL/IGN/ERR now re-export the host_env names. Wasm keeps local SIG_* literals. Assisted-by: Claude
1 parent 8fda0ec commit fe45a7a

7 files changed

Lines changed: 64 additions & 72 deletions

File tree

‎crates/host_env/src/ctypes.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@ pub const RTLD_LOCAL: i32 = 0;
372372
pub const RTLD_GLOBAL: i32 = 0;
373373
pub const SIZEOF_TIME_T: usize = core::mem::size_of::<TimeT>();
374374

375+
/// `_ctypes` call flags (`_flags_` on a function pointer).
376+
pub const FUNCFLAG_STDCALL: u32 = 0x0;
377+
pub const FUNCFLAG_CDECL: u32 = 0x1;
378+
pub const FUNCFLAG_HRESULT: u32 = 0x2;
379+
pub const FUNCFLAG_PYTHONAPI: u32 = 0x4;
380+
pub const FUNCFLAG_USE_ERRNO: u32 = 0x8;
381+
pub const FUNCFLAG_USE_LASTERROR: u32 = 0x10;
382+
pub const TYPEFLAG_ISPOINTER: u32 = 0x100;
383+
pub const TYPEFLAG_HASPOINTER: u32 = 0x200;
384+
pub const DICTFLAG_FINAL: u32 = 0x1000;
385+
pub const CTYPES_MAX_ARGCOUNT: usize = 1024;
386+
375387
#[cfg(all(unix, not(target_os = "wasi")))]
376388
pub fn dlopen_mode(load_flags: Option<i32>) -> i32 {
377389
load_flags.unwrap_or(libc::RTLD_NOW | libc::RTLD_LOCAL) | libc::RTLD_NOW

‎crates/host_env/src/os.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,31 @@ pub const R_OK: u8 = 4;
3535
pub const W_OK: u8 = 2;
3636
pub const X_OK: u8 = 1;
3737

38+
#[cfg(any(unix, target_os = "wasi"))]
39+
pub use libc::AT_FDCWD;
40+
#[cfg(not(any(unix, target_os = "wasi")))]
41+
pub const AT_FDCWD: i32 = -100;
42+
43+
/// BSD `chflags` bits the `libc` crate does not bind. Published by `_stat`
44+
/// on every platform.
45+
pub const UF_SETTABLE: u32 = 0x0000ffff;
46+
pub const UF_NOUNLINK: u32 = 0x00000010;
47+
pub const UF_TRACKED: u32 = 0x00000040;
48+
pub const UF_DATAVAULT: u32 = 0x00000080;
49+
pub const SF_NOUNLINK: u32 = 0x00100000;
50+
pub const SF_SNAPSHOT: u32 = 0x00200000;
51+
pub const SF_FIRMLINK: u32 = 0x00800000;
52+
pub const SF_DATALESS: u32 = 0x40000000;
53+
pub const SF_SETTABLE: u32 = if cfg!(target_os = "macos") {
54+
0x3fff0000
55+
} else {
56+
0xffff0000
57+
};
58+
#[cfg(target_os = "macos")]
59+
pub const SF_SUPPORTED: u32 = 0x009f0000;
60+
#[cfg(target_os = "macos")]
61+
pub const SF_SYNTHETIC: u32 = 0xc0000000;
62+
3863
#[cfg(not(any(unix, windows, target_os = "wasi")))]
3964
pub fn rename(
4065
from: impl AsRef<std::path::Path>,

‎crates/host_env/src/signal.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ use crate::os::CheckLibcZero;
1010
#[cfg(any(unix, windows))]
1111
pub use libc::sighandler_t;
1212

13+
#[cfg(unix)]
14+
pub use libc::{SIG_DFL, SIG_ERR, SIG_IGN};
15+
16+
#[cfg(not(unix))]
17+
pub const SIG_DFL: libc::sighandler_t = 0;
18+
#[cfg(not(unix))]
19+
pub const SIG_IGN: libc::sighandler_t = 1;
20+
#[cfg(not(unix))]
21+
pub const SIG_ERR: libc::sighandler_t = -1 as _;
22+
1323
#[cfg(all(unix, not(target_os = "android")))]
1424
pub use libc::{ITIMER_PROF, ITIMER_REAL, ITIMER_VIRTUAL};
1525

‎crates/vm/src/stdlib/_ctypes.rs‎

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -248,28 +248,11 @@ pub(crate) mod _ctypes {
248248
const SIZEOF_TIME_T: usize = rustpython_host_env::ctypes::SIZEOF_TIME_T;
249249

250250
#[pyattr]
251-
const CTYPES_MAX_ARGCOUNT: usize = 1024;
252-
253-
#[pyattr]
254-
const FUNCFLAG_STDCALL: u32 = 0x0;
255-
#[pyattr]
256-
const FUNCFLAG_CDECL: u32 = 0x1;
257-
#[pyattr]
258-
const FUNCFLAG_HRESULT: u32 = 0x2;
259-
#[pyattr]
260-
const FUNCFLAG_PYTHONAPI: u32 = 0x4;
261-
#[pyattr]
262-
const FUNCFLAG_USE_ERRNO: u32 = 0x8;
263-
#[pyattr]
264-
const FUNCFLAG_USE_LASTERROR: u32 = 0x10;
265-
266-
#[pyattr]
267-
const TYPEFLAG_ISPOINTER: u32 = 0x100;
268-
#[pyattr]
269-
const TYPEFLAG_HASPOINTER: u32 = 0x200;
270-
271-
#[pyattr]
272-
const DICTFLAG_FINAL: u32 = 0x1000;
251+
use rustpython_host_env::ctypes::{
252+
CTYPES_MAX_ARGCOUNT, DICTFLAG_FINAL, FUNCFLAG_CDECL, FUNCFLAG_HRESULT, FUNCFLAG_PYTHONAPI,
253+
FUNCFLAG_STDCALL, FUNCFLAG_USE_ERRNO, FUNCFLAG_USE_LASTERROR, TYPEFLAG_HASPOINTER,
254+
TYPEFLAG_ISPOINTER,
255+
};
273256

274257
#[pyattr(name = "ArgumentError", once)]
275258
fn argument_error(vm: &VirtualMachine) -> PyTypeRef {

‎crates/vm/src/stdlib/_signal.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,28 @@ pub(crate) mod _signal {
7373
}
7474
}
7575

76-
#[cfg(unix)]
76+
#[cfg(any(unix, windows))]
7777
#[allow(unused_imports)]
78-
pub use libc::SIG_ERR;
78+
pub use host_signal::SIG_ERR;
7979

80-
#[cfg(unix)]
80+
#[cfg(any(unix, windows))]
8181
#[pyattr]
82-
pub use libc::{SIG_DFL, SIG_IGN};
82+
pub use host_signal::{SIG_DFL, SIG_IGN};
8383

8484
// pthread_sigmask 'how' constants
8585
#[cfg(unix)]
8686
#[pyattr]
8787
use libc::{SIG_BLOCK, SIG_SETMASK, SIG_UNBLOCK};
8888

89-
#[cfg(not(unix))]
89+
#[cfg(not(any(unix, windows)))]
9090
#[pyattr]
9191
pub const SIG_DFL: sighandler_t = 0;
9292

93-
#[cfg(not(unix))]
93+
#[cfg(not(any(unix, windows)))]
9494
#[pyattr]
9595
pub const SIG_IGN: sighandler_t = 1;
9696

97-
#[cfg(not(unix))]
97+
#[cfg(not(any(unix, windows)))]
9898
#[allow(dead_code)]
9999
pub const SIG_ERR: sighandler_t = -1 as _;
100100

‎crates/vm/src/stdlib/_stat.rs‎

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -319,45 +319,14 @@ mod _stat {
319319
);
320320

321321
#[pyattr]
322-
pub const SF_SETTABLE: u32 = if cfg!(target_os = "macos") {
323-
0x3fff0000
324-
} else {
325-
0xffff0000
322+
pub use rustpython_host_env::os::{
323+
SF_DATALESS, SF_FIRMLINK, SF_NOUNLINK, SF_SETTABLE, SF_SNAPSHOT, UF_DATAVAULT, UF_NOUNLINK,
324+
UF_SETTABLE, UF_TRACKED,
326325
};
327326

328-
#[pyattr]
329-
pub const UF_SETTABLE: u32 = 0x0000ffff;
330-
331-
#[pyattr]
332-
pub const UF_NOUNLINK: u32 = 0x00000010;
333-
334-
#[pyattr]
335-
pub const UF_TRACKED: u32 = 0x00000040;
336-
337-
#[pyattr]
338-
pub const UF_DATAVAULT: u32 = 0x00000080;
339-
340-
#[pyattr]
341-
pub const SF_NOUNLINK: u32 = 0x00100000;
342-
343-
#[pyattr]
344-
pub const SF_SNAPSHOT: u32 = 0x00200000;
345-
346-
#[pyattr]
347-
pub const SF_FIRMLINK: u32 = 0x00800000;
348-
349-
#[pyattr]
350-
pub const SF_DATALESS: u32 = 0x40000000;
351-
352-
// MacOS specific
353-
354-
#[cfg(target_os = "macos")]
355-
#[pyattr]
356-
pub const SF_SUPPORTED: u32 = 0x009f0000;
357-
358327
#[cfg(target_os = "macos")]
359328
#[pyattr]
360-
pub const SF_SYNTHETIC: u32 = 0xc0000000;
329+
pub use rustpython_host_env::os::{SF_SUPPORTED, SF_SYNTHETIC};
361330

362331
// Stat result indices
363332

‎crates/vm/src/stdlib/os.rs‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@ pub struct TargetIsDirectory {
3030
pub(crate) target_is_directory: bool,
3131
}
3232

33-
cfg_select! {
34-
any(unix, target_os = "wasi") => {
35-
use libc::AT_FDCWD;
36-
}
37-
_ => {
38-
const AT_FDCWD: i32 = -100;
39-
}
40-
}
33+
use crate::host_env::os::AT_FDCWD;
4134

4235
const DEFAULT_DIR_FD: crt_fd::Borrowed<'static> = unsafe { crt_fd::Borrowed::borrow_raw(AT_FDCWD) };
4336

0 commit comments

Comments
 (0)