From 468f1aa31236fd43120d09b6f8c995a70460a423 Mon Sep 17 00:00:00 2001 From: Marvin Mednick Date: Wed, 15 Feb 2023 16:50:10 -0800 Subject: [PATCH 1/2] Implmentation for os.pathconf_names --- extra_tests/snippets/stdlib_os.py | 8 ++++++++ vm/src/stdlib/posix.rs | 22 ++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/extra_tests/snippets/stdlib_os.py b/extra_tests/snippets/stdlib_os.py index 4c0d28e3cb..26109fabad 100644 --- a/extra_tests/snippets/stdlib_os.py +++ b/extra_tests/snippets/stdlib_os.py @@ -507,3 +507,11 @@ def __exit__(self, exc_type, exc_val, exc_tb): for arg in [None, 1, 1.0, TabError]: assert_raises(TypeError, os.system, arg) + +# Testing for os.pathconf_names +if not sys.platform.startswith("win"): + assert len(os.pathconf_names) > 0 + assert 'PC_NAME_MAX' in os.pathconf_names + for option,index in os.pathconf_names.items(): + assert os.pathconf('/', index) == os.pathconf('/', option) + diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs index 7f14b2db56..13ea3fec41 100644 --- a/vm/src/stdlib/posix.rs +++ b/vm/src/stdlib/posix.rs @@ -50,8 +50,10 @@ pub mod module { ffi::{CStr, CString}, fs, io, os::unix::{ffi as ffi_ext, io::RawFd}, + str::FromStr, }; - use strum_macros::EnumString; + use strum::VariantNames; + use strum_macros::{EnumString, EnumVariantNames}; #[pyattr] use libc::{PRIO_PGRP, PRIO_PROCESS, PRIO_USER}; @@ -1681,7 +1683,7 @@ pub mod module { // Copy from [nix::unistd::PathconfVar](https://docs.rs/nix/0.21.0/nix/unistd/enum.PathconfVar.html) // Change enum name to fit python doc - #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, EnumString)] + #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, EnumString, EnumVariantNames)] #[repr(i32)] #[allow(non_camel_case_types)] pub enum PathconfVar { @@ -1881,6 +1883,22 @@ pub mod module { pathconf(PathOrFd::Fd(fd), name, vm) } + #[pyattr] + fn pathconf_names(vm: &VirtualMachine) -> PyDictRef { + let pathname = vm.ctx.new_dict(); + for variant in PathconfVar::VARIANTS { + // get the name of variant as a string to use as the dictionary key + let key = vm.ctx.new_str(variant.to_string()); + // get the enum from the string and convert it to an integer for the dictionary value + let value: PyObjectRef = vm + .ctx + .new_int(PathconfVar::from_str(variant).unwrap() as u8) + .into(); + pathname.set_item(&*key, value, vm).unwrap(); + } + pathname + } + #[cfg(any(target_os = "linux", target_os = "macos"))] #[derive(FromArgs)] struct SendFileArgs { From 955347e42601e97b6356599f627add500c545da5 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 21 Feb 2023 19:17:10 +0900 Subject: [PATCH 2/2] posix.pathconf_names only for linux --- extra_tests/snippets/stdlib_os.py | 3 +-- vm/src/stdlib/posix.rs | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/extra_tests/snippets/stdlib_os.py b/extra_tests/snippets/stdlib_os.py index 26109fabad..6eadeb19e0 100644 --- a/extra_tests/snippets/stdlib_os.py +++ b/extra_tests/snippets/stdlib_os.py @@ -509,9 +509,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): assert_raises(TypeError, os.system, arg) # Testing for os.pathconf_names -if not sys.platform.startswith("win"): +if sys.platform.startswith('linux'): assert len(os.pathconf_names) > 0 assert 'PC_NAME_MAX' in os.pathconf_names for option,index in os.pathconf_names.items(): assert os.pathconf('/', index) == os.pathconf('/', option) - diff --git a/vm/src/stdlib/posix.rs b/vm/src/stdlib/posix.rs index 13ea3fec41..ea3e6b9d8d 100644 --- a/vm/src/stdlib/posix.rs +++ b/vm/src/stdlib/posix.rs @@ -50,9 +50,7 @@ pub mod module { ffi::{CStr, CString}, fs, io, os::unix::{ffi as ffi_ext, io::RawFd}, - str::FromStr, }; - use strum::VariantNames; use strum_macros::{EnumString, EnumVariantNames}; #[pyattr] @@ -1883,8 +1881,12 @@ pub mod module { pathconf(PathOrFd::Fd(fd), name, vm) } + // TODO: this is expected to be run on macOS as a unix, but somehow not. + #[cfg(target_os = "linux")] #[pyattr] fn pathconf_names(vm: &VirtualMachine) -> PyDictRef { + use std::str::FromStr; + use strum::VariantNames; let pathname = vm.ctx.new_dict(); for variant in PathconfVar::VARIANTS { // get the name of variant as a string to use as the dictionary key