Skip to main content

rustpython_vm/stdlib/
posix_unix_like.rs

1#[pymodule(sub, name = "posix")]
2pub(crate) mod _posix_unix_like {
3    use rustpython_host_env::os::ffi::OsStringExt;
4
5    use crate::{
6        PyObjectRef, PyResult, VirtualMachine,
7        builtins::PyDictRef,
8        exceptions::OSErrorBuilder,
9        ospath::OsPath,
10        stdlib::os::{_os, DirFd},
11    };
12
13    #[pyattr]
14    fn environ(vm: &VirtualMachine) -> PyDictRef {
15        let environ = vm.ctx.new_dict();
16        for (key, value) in crate::host_env::os::vars_os() {
17            let key: PyObjectRef = vm.ctx.new_bytes(key.into_vec()).into();
18            let value: PyObjectRef = vm.ctx.new_bytes(value.into_vec()).into();
19            environ.set_item(&*key, value, vm).unwrap();
20        }
21
22        environ
23    }
24
25    #[derive(FromArgs)]
26    struct RemoveArgs<'a> {
27        #[pyarg(any)]
28        path: OsPath,
29        #[pyarg(flatten)]
30        dir_fd: DirFd<'a, { _os::UNLINK_DIR_FD as usize }>,
31    }
32
33    #[pyfunction]
34    #[pyfunction(name = "unlink")]
35    fn remove(args: RemoveArgs<'_>, vm: &VirtualMachine) -> PyResult<()> {
36        rustpython_host_env::posix::unlinkat(args.dir_fd.get_opt(), &args.path)
37            .map_err(|err| OSErrorBuilder::with_filename(&err, args.path, vm))
38    }
39}