From 4729ca3af0ba63644ddf88b1cd0a5137394e9bee Mon Sep 17 00:00:00 2001 From: "Jeong, YunWon" Date: Thu, 28 Dec 2023 23:05:41 +0900 Subject: [PATCH] Drop winapi from rustpython-vm --- Cargo.lock | 1 - vm/Cargo.toml | 5 +---- vm/src/stdlib/errno.rs | 25 ++++++++++++++++++++++--- vm/src/stdlib/nt.rs | 7 +++---- vm/src/stdlib/os.rs | 19 +++++++++---------- vm/src/stdlib/time.rs | 29 +++++++++-------------------- vm/src/stdlib/winreg.rs | 6 +++--- 7 files changed, 47 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3ce858025..2d4c8a6ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2287,7 +2287,6 @@ dependencies = [ "wasm-bindgen", "which", "widestring", - "winapi", "windows", "windows-sys 0.52.0", "winreg", diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 7bdf3617e..f061f54a8 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -132,6 +132,7 @@ features = [ "Win32_System_Diagnostics_Debug", "Win32_System_LibraryLoader", "Win32_System_Memory", + "Win32_System_Performance", "Win32_System_Pipes", "Win32_System_Registry", "Win32_System_SystemInformation", @@ -141,10 +142,6 @@ features = [ "Win32_UI_WindowsAndMessaging", ] -[target.'cfg(windows)'.dependencies.winapi] -version = "0.3.9" -features = ["std", "winnt"] - [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = "0.2.80" diff --git a/vm/src/stdlib/errno.rs b/vm/src/stdlib/errno.rs index fe36b4965..a142d68a3 100644 --- a/vm/src/stdlib/errno.rs +++ b/vm/src/stdlib/errno.rs @@ -24,12 +24,31 @@ pub fn make_module(vm: &VirtualMachine) -> PyRef { pub mod errors { pub use libc::*; #[cfg(windows)] - pub use winapi::shared::winerror::*; + pub use windows_sys::Win32::{ + Foundation::*, + Networking::WinSock::{ + WSABASEERR, WSADESCRIPTION_LEN, WSAEACCES, WSAEADDRINUSE, WSAEADDRNOTAVAIL, + WSAEAFNOSUPPORT, WSAEALREADY, WSAEBADF, WSAECANCELLED, WSAECONNABORTED, + WSAECONNREFUSED, WSAECONNRESET, WSAEDESTADDRREQ, WSAEDISCON, WSAEDQUOT, WSAEFAULT, + WSAEHOSTDOWN, WSAEHOSTUNREACH, WSAEINPROGRESS, WSAEINTR, WSAEINVAL, + WSAEINVALIDPROCTABLE, WSAEINVALIDPROVIDER, WSAEISCONN, WSAELOOP, WSAEMFILE, + WSAEMSGSIZE, WSAENAMETOOLONG, WSAENETDOWN, WSAENETRESET, WSAENETUNREACH, WSAENOBUFS, + WSAENOMORE, WSAENOPROTOOPT, WSAENOTCONN, WSAENOTEMPTY, WSAENOTSOCK, WSAEOPNOTSUPP, + WSAEPFNOSUPPORT, WSAEPROCLIM, WSAEPROTONOSUPPORT, WSAEPROTOTYPE, + WSAEPROVIDERFAILEDINIT, WSAEREFUSED, WSAEREMOTE, WSAESHUTDOWN, WSAESOCKTNOSUPPORT, + WSAESTALE, WSAETIMEDOUT, WSAETOOMANYREFS, WSAEUSERS, WSAEWOULDBLOCK, WSAHOST_NOT_FOUND, + WSAID_ACCEPTEX, WSAID_CONNECTEX, WSAID_DISCONNECTEX, WSAID_GETACCEPTEXSOCKADDRS, + WSAID_TRANSMITFILE, WSAID_TRANSMITPACKETS, WSAID_WSAPOLL, WSAID_WSARECVMSG, + WSANOTINITIALISED, WSANO_DATA, WSANO_RECOVERY, WSAPROTOCOL_LEN, WSASERVICE_NOT_FOUND, + WSASYSCALLFAILURE, WSASYSNOTREADY, WSASYS_STATUS_LEN, WSATRY_AGAIN, WSATYPE_NOT_FOUND, + WSAVERNOTSUPPORTED, + }, + }; #[cfg(windows)] macro_rules! reexport_wsa { ($($errname:ident),*$(,)?) => { paste::paste! { - $(pub const $errname: i32 = [] as i32;)* + $(pub const $errname: i32 = windows_sys::Win32::Networking::WinSock:: [] as i32;)* } } } @@ -43,7 +62,7 @@ pub mod errors { // TODO: EBADF should be here once winerrs are translated to errnos but it messes up some things atm } #[cfg(windows)] - pub const WSAHOS: i32 = WSAHOST_NOT_FOUND as i32; + pub const WSAHOS: i32 = WSAHOST_NOT_FOUND; } #[cfg(any(unix, windows, target_os = "wasi"))] diff --git a/vm/src/stdlib/nt.rs b/vm/src/stdlib/nt.rs index e55aed883..206faa82e 100644 --- a/vm/src/stdlib/nt.rs +++ b/vm/src/stdlib/nt.rs @@ -250,7 +250,7 @@ pub(crate) mod module { #[pyfunction] fn _getfullpathname(path: OsPath, vm: &VirtualMachine) -> PyResult { let wpath = path.to_widecstring(vm)?; - let mut buffer = vec![0u16; winapi::shared::minwindef::MAX_PATH]; + let mut buffer = vec![0u16; Foundation::MAX_PATH as usize]; let ret = unsafe { FileSystem::GetFullPathNameW( wpath.as_ptr(), @@ -283,7 +283,7 @@ pub(crate) mod module { #[pyfunction] fn _getvolumepathname(path: OsPath, vm: &VirtualMachine) -> PyResult { let wide = path.to_widecstring(vm)?; - let buflen = std::cmp::max(wide.len(), winapi::shared::minwindef::MAX_PATH); + let buflen = std::cmp::max(wide.len(), Foundation::MAX_PATH as usize); let mut buffer = vec![0u16; buflen]; let ret = unsafe { FileSystem::GetVolumePathNameW(wide.as_ptr(), buffer.as_mut_ptr(), buflen as _) @@ -335,7 +335,6 @@ pub(crate) mod module { #[pyfunction] fn _getdiskusage(path: OsPath, vm: &VirtualMachine) -> PyResult<(u64, u64)> { - use winapi::shared::winerror; use FileSystem::GetDiskFreeSpaceExW; let wpath = path.to_widecstring(vm)?; @@ -348,7 +347,7 @@ pub(crate) mod module { return Ok((total, free)); } let err = io::Error::last_os_error(); - if err.raw_os_error() == Some(winerror::ERROR_DIRECTORY as i32) { + if err.raw_os_error() == Some(Foundation::ERROR_DIRECTORY as i32) { if let Some(parent) = path.as_ref().parent() { let parent = widestring::WideCString::from_os_str(parent).unwrap(); diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 94b1938e4..376c18fb3 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -1200,22 +1200,20 @@ pub(super) mod _os { let res = unsafe { suppress_iph!(libc::lseek(fd, position, how)) }; #[cfg(windows)] let res = unsafe { - use winapi::um::winnt; use windows_sys::Win32::Storage::FileSystem; let handle = Fd(fd).to_raw_handle().map_err(|e| e.into_pyexception(vm))?; - let mut li = winnt::LARGE_INTEGER::default(); - *li.QuadPart_mut() = position; + let mut distance_to_move: [i32; 2] = std::mem::transmute(position); let ret = FileSystem::SetFilePointer( handle as _, - li.u().LowPart as _, - &mut li.u_mut().HighPart, + distance_to_move[0], + &mut distance_to_move[1], how as _, ); if ret == FileSystem::INVALID_SET_FILE_POINTER { -1 } else { - li.u_mut().LowPart = ret; - *li.QuadPart() + distance_to_move[0] = ret as _; + std::mem::transmute(distance_to_move) } }; if res < 0 { @@ -1358,7 +1356,7 @@ pub(super) mod _os { #[cfg(windows)] { use std::{fs::OpenOptions, os::windows::prelude::*}; - use winapi::shared::minwindef::DWORD; + type DWORD = u32; use windows_sys::Win32::{Foundation::FILETIME, Storage::FileSystem}; let [] = dir_fd.0; @@ -1605,9 +1603,10 @@ pub(super) mod _os { if #[cfg(any(target_os = "android", target_os = "redox"))] { Ok(Some("UTF-8".to_owned())) } else if #[cfg(windows)] { + use windows_sys::Win32::System::Console; let cp = match fd { - 0 => unsafe { winapi::um::consoleapi::GetConsoleCP() }, - 1 | 2 => unsafe { winapi::um::consoleapi::GetConsoleOutputCP() }, + 0 => unsafe { Console::GetConsoleCP() }, + 1 | 2 => unsafe { Console::GetConsoleOutputCP() }, _ => 0, }; diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 883ef3342..9717a6966 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -628,43 +628,33 @@ mod platform { PyRef, PyResult, VirtualMachine, }; use std::time::Duration; - use winapi::shared::ntdef::ULARGE_INTEGER; - use winapi::um::profileapi::{QueryPerformanceCounter, QueryPerformanceFrequency}; use windows_sys::Win32::{ Foundation::FILETIME, + System::Performance::{QueryPerformanceCounter, QueryPerformanceFrequency}, System::SystemInformation::{GetSystemTimeAdjustment, GetTickCount64}, System::Threading::{GetCurrentProcess, GetCurrentThread, GetProcessTimes, GetThreadTimes}, }; fn u64_from_filetime(time: FILETIME) -> u64 { - unsafe { - let mut large = std::mem::MaybeUninit::::uninit(); - { - let m = (*large.as_mut_ptr()).u_mut(); - m.LowPart = time.dwLowDateTime; - m.HighPart = time.dwHighDateTime; - } - let large = large.assume_init(); - *large.QuadPart() - } + let large: [u32; 2] = [time.dwLowDateTime, time.dwHighDateTime]; + unsafe { std::mem::transmute(large) } } fn win_perf_counter_frequency(vm: &VirtualMachine) -> PyResult { - let freq = unsafe { + let frequency = unsafe { let mut freq = std::mem::MaybeUninit::uninit(); if QueryPerformanceFrequency(freq.as_mut_ptr()) == 0 { return Err(errno_err(vm)); } freq.assume_init() }; - let frequency = unsafe { freq.QuadPart() }; - if *frequency < 1 { + if frequency < 1 { Err(vm.new_runtime_error("invalid QueryPerformanceFrequency".to_owned())) - } else if *frequency > i64::MAX / SEC_TO_NS { + } else if frequency > i64::MAX / SEC_TO_NS { Err(vm.new_overflow_error("QueryPerformanceFrequency is too large".to_owned())) } else { - Ok(*frequency) + Ok(frequency) } } @@ -678,15 +668,14 @@ mod platform { } pub(super) fn get_perf_time(vm: &VirtualMachine) -> PyResult { - let now = unsafe { + let ticks = unsafe { let mut performance_count = std::mem::MaybeUninit::uninit(); QueryPerformanceCounter(performance_count.as_mut_ptr()); performance_count.assume_init() }; - let ticks = unsafe { now.QuadPart() }; Ok(Duration::from_nanos(time_muldiv( - *ticks, + ticks, SEC_TO_NS, global_frequency(vm)?, ))) diff --git a/vm/src/stdlib/winreg.rs b/vm/src/stdlib/winreg.rs index b31694f9e..b992de9cb 100644 --- a/vm/src/stdlib/winreg.rs +++ b/vm/src/stdlib/winreg.rs @@ -34,7 +34,7 @@ mod winreg { }; use ::winreg::{enums::RegType, RegKey, RegValue}; use std::{ffi::OsStr, io}; - use winapi::shared::winerror; + use windows_sys::Win32::Foundation; // access rights #[pyattr] @@ -201,7 +201,7 @@ mod winreg { key.with_key(|k| k.enum_keys().nth(index as usize)) .unwrap_or_else(|| { Err(io::Error::from_raw_os_error( - winerror::ERROR_NO_MORE_ITEMS as i32, + Foundation::ERROR_NO_MORE_ITEMS as i32, )) }) .map_err(|e| e.to_pyexception(vm)) @@ -217,7 +217,7 @@ mod winreg { .with_key(|k| k.enum_values().nth(index as usize)) .unwrap_or_else(|| { Err(io::Error::from_raw_os_error( - winerror::ERROR_NO_MORE_ITEMS as i32, + Foundation::ERROR_NO_MORE_ITEMS as i32, )) }) .map_err(|e| e.to_pyexception(vm))?;