MCPcopy Create free account
hub / github.com/RustPython/RustPython / getwindowsversion

Function getwindowsversion

crates/vm/src/stdlib/sys.rs:1161–1207  ·  view source on GitHub ↗
(vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

1159 #[cfg(windows)]
1160 #[pyfunction]
1161 fn getwindowsversion(vm: &VirtualMachine) -> PyResult<crate::builtins::tuple::PyTupleRef> {
1162 use std::ffi::OsString;
1163 use std::os::windows::ffi::OsStringExt;
1164 use windows_sys::Win32::System::SystemInformation::{
1165 GetVersionExW, OSVERSIONINFOEXW, OSVERSIONINFOW,
1166 };
1167
1168 let mut version: OSVERSIONINFOEXW = unsafe { core::mem::zeroed() };
1169 version.dwOSVersionInfoSize = core::mem::size_of::<OSVERSIONINFOEXW>() as u32;
1170 let result = unsafe {
1171 let os_vi = &mut version as *mut OSVERSIONINFOEXW as *mut OSVERSIONINFOW;
1172 // SAFETY: GetVersionExW accepts a pointer of OSVERSIONINFOW, but windows-sys crate's type currently doesn't allow to do so.
1173 // https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexw#parameters
1174 GetVersionExW(os_vi)
1175 };
1176
1177 if result == 0 {
1178 return Err(vm.new_os_error("failed to get windows version".to_owned()));
1179 }
1180
1181 let service_pack = {
1182 let (last, _) = version
1183 .szCSDVersion
1184 .iter()
1185 .take_while(|&x| x != &0)
1186 .enumerate()
1187 .last()
1188 .unwrap_or((0, &0));
1189 let sp = OsString::from_wide(&version.szCSDVersion[..last]);
1190 sp.into_string()
1191 .map_err(|_| vm.new_os_error("service pack is not ASCII".to_owned()))?
1192 };
1193 let real_version = get_kernel32_version().map_err(|e| vm.new_os_error(e.to_string()))?;
1194 let winver = WindowsVersionData {
1195 major: real_version.0,
1196 minor: real_version.1,
1197 build: real_version.2,
1198 platform: version.dwPlatformId,
1199 service_pack,
1200 service_pack_major: version.wServicePackMajor,
1201 service_pack_minor: version.wServicePackMinor,
1202 suite_mask: version.wSuiteMask,
1203 product_type: version.wProductType,
1204 platform_version: (real_version.0, real_version.1, real_version.2), // TODO Provide accurate version, like CPython impl
1205 };
1206 Ok(PyWindowsVersion::from_data(winver, vm))
1207 }
1208
1209 fn _unraisablehook(unraisable: UnraisableHookArgsData, vm: &VirtualMachine) -> PyResult<()> {
1210 use super::PyStderr;

Callers 1

_win32_verFunction · 0.90

Calls 8

get_kernel32_versionFunction · 0.85
new_os_errorMethod · 0.80
into_stringMethod · 0.80
to_stringMethod · 0.80
ErrClass · 0.50
to_ownedMethod · 0.45
lastMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected