forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposix_compat.rs
More file actions
68 lines (57 loc) · 1.94 KB
/
posix_compat.rs
File metadata and controls
68 lines (57 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! `posix` compatible module for `not(any(unix, windows))`
use crate::{PyObjectRef, VirtualMachine};
pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let module = module::make_module(vm);
super::os::extend_module(vm, &module);
module
}
#[pymodule(name = "posix")]
pub(crate) mod module {
use crate::{
builtins::PyStrRef,
stdlib::os::{DirFd, PyPathLike, SupportFunc, TargetIsDirectory, _os},
PyObjectRef, PyResult, VirtualMachine,
};
use std::env;
#[cfg(unix)]
use std::os::unix::ffi as ffi_ext;
#[cfg(target_os = "wasi")]
use std::os::wasi::ffi as ffi_ext;
#[pyfunction]
pub(super) fn access(_path: PyStrRef, _mode: u8, vm: &VirtualMachine) -> PyResult<bool> {
os_unimpl("os.access", vm)
}
#[derive(FromArgs)]
#[allow(unused)]
pub(super) struct SymlinkArgs {
src: PyPathLike,
dst: PyPathLike,
#[pyarg(flatten)]
_target_is_directory: TargetIsDirectory,
#[pyarg(flatten)]
_dir_fd: DirFd<{ _os::SYMLINK_DIR_FD as usize }>,
}
#[pyfunction]
pub(super) fn symlink(_args: SymlinkArgs, vm: &VirtualMachine) -> PyResult<()> {
os_unimpl("os.symlink", vm)
}
#[cfg(target_os = "wasi")]
#[pyattr]
fn environ(vm: &VirtualMachine) -> crate::builtins::PyDictRef {
use ffi_ext::OsStringExt;
let environ = vm.ctx.new_dict();
for (key, value) in env::vars_os() {
let key: PyObjectRef = vm.ctx.new_bytes(key.into_vec()).into();
let value: PyObjectRef = vm.ctx.new_bytes(value.into_vec()).into();
environ.set_item(&*key, value, vm).unwrap();
}
environ
}
#[allow(dead_code)]
fn os_unimpl<T>(func: &str, vm: &VirtualMachine) -> PyResult<T> {
Err(vm.new_os_error(format!("{} is not supported on this platform", func)))
}
pub(crate) fn support_funcs() -> Vec<SupportFunc> {
Vec::new()
}
}