Skip to content

Commit 351d464

Browse files
authored
Merge pull request #4571 from youknowone/posix-pathconf_names
posix.pathconf_names
2 parents 6385b82 + fcacdb2 commit 351d464

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

extra_tests/snippets/stdlib_os.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,12 @@ def __exit__(self, exc_type, exc_val, exc_tb):
509509
assert_raises(TypeError, os.system, arg)
510510

511511
# Testing for os.pathconf_names
512-
if sys.platform.startswith('linux'):
512+
if not sys.platform.startswith('win'):
513513
assert len(os.pathconf_names) > 0
514514
assert 'PC_NAME_MAX' in os.pathconf_names
515-
for option,index in os.pathconf_names.items():
515+
for option, index in os.pathconf_names.items():
516+
if sys.platform == "darwin":
517+
# TODO: check why it fails
518+
if option in ["PC_MAX_CANON", "PC_MAX_INPUT", "PC_VDISABLE"]:
519+
continue
516520
assert os.pathconf('/', index) == os.pathconf('/', option)

vm/src/stdlib/posix.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub mod module {
5151
fs, io,
5252
os::unix::{ffi as ffi_ext, io::RawFd},
5353
};
54-
use strum_macros::{EnumString, EnumVariantNames};
54+
use strum_macros::{EnumIter, EnumString};
5555

5656
#[pyattr]
5757
use libc::{PRIO_PGRP, PRIO_PROCESS, PRIO_USER};
@@ -1681,7 +1681,7 @@ pub mod module {
16811681

16821682
// Copy from [nix::unistd::PathconfVar](https://docs.rs/nix/0.21.0/nix/unistd/enum.PathconfVar.html)
16831683
// Change enum name to fit python doc
1684-
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, EnumString, EnumVariantNames)]
1684+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, EnumIter, EnumString)]
16851685
#[repr(i32)]
16861686
#[allow(non_camel_case_types)]
16871687
pub enum PathconfVar {
@@ -1856,6 +1856,7 @@ pub mod module {
18561856
use nix::errno::{self, Errno};
18571857

18581858
Errno::clear();
1859+
debug_assert_eq!(errno::errno(), 0);
18591860
let raw = match path {
18601861
PathOrFd::Path(path) => {
18611862
let path = CString::new(path.into_bytes())
@@ -1881,22 +1882,18 @@ pub mod module {
18811882
pathconf(PathOrFd::Fd(fd), name, vm)
18821883
}
18831884

1884-
// TODO: this is expected to be run on macOS as a unix, but somehow not.
1885-
#[cfg(target_os = "linux")]
18861885
#[pyattr]
18871886
fn pathconf_names(vm: &VirtualMachine) -> PyDictRef {
1888-
use std::str::FromStr;
1889-
use strum::VariantNames;
1887+
use strum::IntoEnumIterator;
18901888
let pathname = vm.ctx.new_dict();
1891-
for variant in PathconfVar::VARIANTS {
1889+
for variant in PathconfVar::iter() {
18921890
// get the name of variant as a string to use as the dictionary key
1893-
let key = vm.ctx.new_str(variant.to_string());
1891+
let key = vm.ctx.new_str(format!("{:?}", variant));
18941892
// get the enum from the string and convert it to an integer for the dictionary value
1895-
let value: PyObjectRef = vm
1896-
.ctx
1897-
.new_int(PathconfVar::from_str(variant).unwrap() as u8)
1898-
.into();
1899-
pathname.set_item(&*key, value, vm).unwrap();
1893+
let value = vm.ctx.new_int(variant as u8);
1894+
pathname
1895+
.set_item(&*key, value.into(), vm)
1896+
.expect("dict set_item unexpectedly failed");
19001897
}
19011898
pathname
19021899
}

0 commit comments

Comments
 (0)