mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Drop winapi from rustpython-vm
This commit is contained in:
1 parent
cccfb08835
commit
4729ca3af0
7 files changed
+47
-45
No files matched your search
Generated
-1
@@ -2287,7 +2287,6 @@ dependencies = [
|
||||
"wasm-bindgen",
|
||||
"which",
|
||||
"widestring",
|
||||
"winapi",
|
||||
"windows",
|
||||
"windows-sys 0.52.0",
|
||||
"winreg",
|
||||
|
||||
+1
-4
@@ -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"
|
||||
|
||||
|
||||
+22
-3
@@ -24,12 +24,31 @@ pub fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
|
||||
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 = [<WSA $errname>] as i32;)*
|
||||
$(pub const $errname: i32 = windows_sys::Win32::Networking::WinSock:: [<WSA $errname>] 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"))]
|
||||
|
||||
+3
-4
@@ -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();
|
||||
|
||||
|
||||
+9
-10
@@ -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,
|
||||
};
|
||||
|
||||
|
||||
+9
-20
@@ -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::<ULARGE_INTEGER>::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<i64> {
|
||||
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<Duration> {
|
||||
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)?,
|
||||
)))
|
||||
|
||||
@@ -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))?;
|
||||
|
||||
Reference in new issue
Block a user