diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index ce3ec191c..ba586e8f1 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -1533,7 +1533,7 @@ mod _os { #[pyimpl(with(PyStructSequence))] impl TimesResult {} - #[cfg(not(target_os = "redox"))] + #[cfg(any(unix, windows))] #[pyfunction] fn times(vm: &VirtualMachine) -> PyResult { #[cfg(windows)] @@ -1710,17 +1710,22 @@ mod _os { #[pyimpl(with(PyStructSequence))] impl UnameResult {} - enum NameOrVal { - Name(String), - Val(i32), - } + #[cfg(unix)] + struct ConfName(i32); - impl TryFromObject for NameOrVal { + #[cfg(unix)] + impl TryFromObject for ConfName { fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { - match obj.downcast::() { - Ok(int) => int::try_to_primitive(int.as_bigint(), vm).map(Self::Val), - Err(obj) => PyStrRef::try_from_object(vm, obj).map(|o| Self::Name(o.to_string())), - } + let i = match obj.downcast::() { + Ok(int) => int::try_to_primitive(int.as_bigint(), vm)?, + Err(obj) => { + let s = PyStrRef::try_from_object(vm, obj)?; + s.as_str().parse::().map_err(|_| { + vm.new_value_error("unrecognized configuration name".to_string()) + })? as i32 + } + }; + Ok(Self(i)) } } @@ -1894,15 +1899,12 @@ mod _os { #[cfg(unix)] #[pyfunction] - fn pathconf(path: PathOrFd, name: NameOrVal, vm: &VirtualMachine) -> PyResult> { + fn pathconf( + path: PathOrFd, + ConfName(name): ConfName, + vm: &VirtualMachine, + ) -> PyResult> { use nix::errno::{self, Errno}; - use std::str::FromStr; - let name = match name { - NameOrVal::Name(s) => PathconfVar::from_str(&s) - .map_err(|_| vm.new_value_error("unrecognized configuration name".to_string()))? - as i32, - NameOrVal::Val(v) => v, - }; Errno::clear(); let raw = match path { @@ -1927,7 +1929,7 @@ mod _os { #[cfg(unix)] #[pyfunction] - fn fpathconf(fd: i32, name: NameOrVal, vm: &VirtualMachine) -> PyResult> { + fn fpathconf(fd: i32, name: ConfName, vm: &VirtualMachine) -> PyResult> { pathconf(PathOrFd::Fd(fd), name, vm) }