Skip to content

Commit d8fe778

Browse files
committed
Implmentation for os.pathconf_names
1 parent 2f47387 commit d8fe778

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

vm/src/stdlib/posix.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ pub mod module {
5050
ffi::{CStr, CString},
5151
fs, io,
5252
os::unix::{ffi as ffi_ext, io::RawFd},
53+
str::FromStr,
5354
};
54-
use strum_macros::EnumString;
55+
use strum::VariantNames;
56+
use strum_macros::{EnumString, EnumVariantNames};
5557

5658
#[pyattr]
5759
use libc::{PRIO_PGRP, PRIO_PROCESS, PRIO_USER};
@@ -1691,7 +1693,7 @@ pub mod module {
16911693

16921694
// Copy from [nix::unistd::PathconfVar](https://docs.rs/nix/0.21.0/nix/unistd/enum.PathconfVar.html)
16931695
// Change enum name to fit python doc
1694-
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, EnumString)]
1696+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, EnumString, EnumVariantNames)]
16951697
#[repr(i32)]
16961698
#[allow(non_camel_case_types)]
16971699
pub enum PathconfVar {
@@ -1891,6 +1893,22 @@ pub mod module {
18911893
pathconf(PathOrFd::Fd(fd), name, vm)
18921894
}
18931895

1896+
#[pyattr]
1897+
fn pathconf_names(vm: &VirtualMachine) -> PyDictRef {
1898+
let pathname = vm.ctx.new_dict();
1899+
for variant in PathconfVar::VARIANTS {
1900+
// get the name of variant as a string to use as the dictionary key
1901+
let key: PyObjectRef = vm.ctx.new_str(variant.to_string()).into();
1902+
// get the enum from the string and convert it to an integer for the dictionary value
1903+
let value: PyObjectRef = vm
1904+
.ctx
1905+
.new_int(PathconfVar::from_str(variant).unwrap() as u8)
1906+
.into();
1907+
pathname.set_item(&*key, value, vm).unwrap();
1908+
}
1909+
pathname
1910+
}
1911+
18941912
#[cfg(any(target_os = "linux", target_os = "macos"))]
18951913
#[derive(FromArgs)]
18961914
struct SendFileArgs {

0 commit comments

Comments
 (0)