Skip to content

Commit 4729ca3

Browse files
committed
Drop winapi from rustpython-vm
1 parent cccfb08 commit 4729ca3

7 files changed

Lines changed: 47 additions & 45 deletions

File tree

‎Cargo.lock‎

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎vm/Cargo.toml‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ features = [
132132
"Win32_System_Diagnostics_Debug",
133133
"Win32_System_LibraryLoader",
134134
"Win32_System_Memory",
135+
"Win32_System_Performance",
135136
"Win32_System_Pipes",
136137
"Win32_System_Registry",
137138
"Win32_System_SystemInformation",
@@ -141,10 +142,6 @@ features = [
141142
"Win32_UI_WindowsAndMessaging",
142143
]
143144

144-
[target.'cfg(windows)'.dependencies.winapi]
145-
version = "0.3.9"
146-
features = ["std", "winnt"]
147-
148145
[target.'cfg(target_arch = "wasm32")'.dependencies]
149146
wasm-bindgen = "0.2.80"
150147

‎vm/src/stdlib/errno.rs‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,31 @@ pub fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
2424
pub mod errors {
2525
pub use libc::*;
2626
#[cfg(windows)]
27-
pub use winapi::shared::winerror::*;
27+
pub use windows_sys::Win32::{
28+
Foundation::*,
29+
Networking::WinSock::{
30+
WSABASEERR, WSADESCRIPTION_LEN, WSAEACCES, WSAEADDRINUSE, WSAEADDRNOTAVAIL,
31+
WSAEAFNOSUPPORT, WSAEALREADY, WSAEBADF, WSAECANCELLED, WSAECONNABORTED,
32+
WSAECONNREFUSED, WSAECONNRESET, WSAEDESTADDRREQ, WSAEDISCON, WSAEDQUOT, WSAEFAULT,
33+
WSAEHOSTDOWN, WSAEHOSTUNREACH, WSAEINPROGRESS, WSAEINTR, WSAEINVAL,
34+
WSAEINVALIDPROCTABLE, WSAEINVALIDPROVIDER, WSAEISCONN, WSAELOOP, WSAEMFILE,
35+
WSAEMSGSIZE, WSAENAMETOOLONG, WSAENETDOWN, WSAENETRESET, WSAENETUNREACH, WSAENOBUFS,
36+
WSAENOMORE, WSAENOPROTOOPT, WSAENOTCONN, WSAENOTEMPTY, WSAENOTSOCK, WSAEOPNOTSUPP,
37+
WSAEPFNOSUPPORT, WSAEPROCLIM, WSAEPROTONOSUPPORT, WSAEPROTOTYPE,
38+
WSAEPROVIDERFAILEDINIT, WSAEREFUSED, WSAEREMOTE, WSAESHUTDOWN, WSAESOCKTNOSUPPORT,
39+
WSAESTALE, WSAETIMEDOUT, WSAETOOMANYREFS, WSAEUSERS, WSAEWOULDBLOCK, WSAHOST_NOT_FOUND,
40+
WSAID_ACCEPTEX, WSAID_CONNECTEX, WSAID_DISCONNECTEX, WSAID_GETACCEPTEXSOCKADDRS,
41+
WSAID_TRANSMITFILE, WSAID_TRANSMITPACKETS, WSAID_WSAPOLL, WSAID_WSARECVMSG,
42+
WSANOTINITIALISED, WSANO_DATA, WSANO_RECOVERY, WSAPROTOCOL_LEN, WSASERVICE_NOT_FOUND,
43+
WSASYSCALLFAILURE, WSASYSNOTREADY, WSASYS_STATUS_LEN, WSATRY_AGAIN, WSATYPE_NOT_FOUND,
44+
WSAVERNOTSUPPORTED,
45+
},
46+
};
2847
#[cfg(windows)]
2948
macro_rules! reexport_wsa {
3049
($($errname:ident),*$(,)?) => {
3150
paste::paste! {
32-
$(pub const $errname: i32 = [<WSA $errname>] as i32;)*
51+
$(pub const $errname: i32 = windows_sys::Win32::Networking::WinSock:: [<WSA $errname>] as i32;)*
3352
}
3453
}
3554
}
@@ -43,7 +62,7 @@ pub mod errors {
4362
// TODO: EBADF should be here once winerrs are translated to errnos but it messes up some things atm
4463
}
4564
#[cfg(windows)]
46-
pub const WSAHOS: i32 = WSAHOST_NOT_FOUND as i32;
65+
pub const WSAHOS: i32 = WSAHOST_NOT_FOUND;
4766
}
4867

4968
#[cfg(any(unix, windows, target_os = "wasi"))]

‎vm/src/stdlib/nt.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ pub(crate) mod module {
250250
#[pyfunction]
251251
fn _getfullpathname(path: OsPath, vm: &VirtualMachine) -> PyResult {
252252
let wpath = path.to_widecstring(vm)?;
253-
let mut buffer = vec![0u16; winapi::shared::minwindef::MAX_PATH];
253+
let mut buffer = vec![0u16; Foundation::MAX_PATH as usize];
254254
let ret = unsafe {
255255
FileSystem::GetFullPathNameW(
256256
wpath.as_ptr(),
@@ -283,7 +283,7 @@ pub(crate) mod module {
283283
#[pyfunction]
284284
fn _getvolumepathname(path: OsPath, vm: &VirtualMachine) -> PyResult {
285285
let wide = path.to_widecstring(vm)?;
286-
let buflen = std::cmp::max(wide.len(), winapi::shared::minwindef::MAX_PATH);
286+
let buflen = std::cmp::max(wide.len(), Foundation::MAX_PATH as usize);
287287
let mut buffer = vec![0u16; buflen];
288288
let ret = unsafe {
289289
FileSystem::GetVolumePathNameW(wide.as_ptr(), buffer.as_mut_ptr(), buflen as _)
@@ -335,7 +335,6 @@ pub(crate) mod module {
335335

336336
#[pyfunction]
337337
fn _getdiskusage(path: OsPath, vm: &VirtualMachine) -> PyResult<(u64, u64)> {
338-
use winapi::shared::winerror;
339338
use FileSystem::GetDiskFreeSpaceExW;
340339

341340
let wpath = path.to_widecstring(vm)?;
@@ -348,7 +347,7 @@ pub(crate) mod module {
348347
return Ok((total, free));
349348
}
350349
let err = io::Error::last_os_error();
351-
if err.raw_os_error() == Some(winerror::ERROR_DIRECTORY as i32) {
350+
if err.raw_os_error() == Some(Foundation::ERROR_DIRECTORY as i32) {
352351
if let Some(parent) = path.as_ref().parent() {
353352
let parent = widestring::WideCString::from_os_str(parent).unwrap();
354353

‎vm/src/stdlib/os.rs‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,22 +1200,20 @@ pub(super) mod _os {
12001200
let res = unsafe { suppress_iph!(libc::lseek(fd, position, how)) };
12011201
#[cfg(windows)]
12021202
let res = unsafe {
1203-
use winapi::um::winnt;
12041203
use windows_sys::Win32::Storage::FileSystem;
12051204
let handle = Fd(fd).to_raw_handle().map_err(|e| e.into_pyexception(vm))?;
1206-
let mut li = winnt::LARGE_INTEGER::default();
1207-
*li.QuadPart_mut() = position;
1205+
let mut distance_to_move: [i32; 2] = std::mem::transmute(position);
12081206
let ret = FileSystem::SetFilePointer(
12091207
handle as _,
1210-
li.u().LowPart as _,
1211-
&mut li.u_mut().HighPart,
1208+
distance_to_move[0],
1209+
&mut distance_to_move[1],
12121210
how as _,
12131211
);
12141212
if ret == FileSystem::INVALID_SET_FILE_POINTER {
12151213
-1
12161214
} else {
1217-
li.u_mut().LowPart = ret;
1218-
*li.QuadPart()
1215+
distance_to_move[0] = ret as _;
1216+
std::mem::transmute(distance_to_move)
12191217
}
12201218
};
12211219
if res < 0 {
@@ -1358,7 +1356,7 @@ pub(super) mod _os {
13581356
#[cfg(windows)]
13591357
{
13601358
use std::{fs::OpenOptions, os::windows::prelude::*};
1361-
use winapi::shared::minwindef::DWORD;
1359+
type DWORD = u32;
13621360
use windows_sys::Win32::{Foundation::FILETIME, Storage::FileSystem};
13631361

13641362
let [] = dir_fd.0;
@@ -1605,9 +1603,10 @@ pub(super) mod _os {
16051603
if #[cfg(any(target_os = "android", target_os = "redox"))] {
16061604
Ok(Some("UTF-8".to_owned()))
16071605
} else if #[cfg(windows)] {
1606+
use windows_sys::Win32::System::Console;
16081607
let cp = match fd {
1609-
0 => unsafe { winapi::um::consoleapi::GetConsoleCP() },
1610-
1 | 2 => unsafe { winapi::um::consoleapi::GetConsoleOutputCP() },
1608+
0 => unsafe { Console::GetConsoleCP() },
1609+
1 | 2 => unsafe { Console::GetConsoleOutputCP() },
16111610
_ => 0,
16121611
};
16131612

‎vm/src/stdlib/time.rs‎

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -628,43 +628,33 @@ mod platform {
628628
PyRef, PyResult, VirtualMachine,
629629
};
630630
use std::time::Duration;
631-
use winapi::shared::ntdef::ULARGE_INTEGER;
632-
use winapi::um::profileapi::{QueryPerformanceCounter, QueryPerformanceFrequency};
633631
use windows_sys::Win32::{
634632
Foundation::FILETIME,
633+
System::Performance::{QueryPerformanceCounter, QueryPerformanceFrequency},
635634
System::SystemInformation::{GetSystemTimeAdjustment, GetTickCount64},
636635
System::Threading::{GetCurrentProcess, GetCurrentThread, GetProcessTimes, GetThreadTimes},
637636
};
638637

639638
fn u64_from_filetime(time: FILETIME) -> u64 {
640-
unsafe {
641-
let mut large = std::mem::MaybeUninit::<ULARGE_INTEGER>::uninit();
642-
{
643-
let m = (*large.as_mut_ptr()).u_mut();
644-
m.LowPart = time.dwLowDateTime;
645-
m.HighPart = time.dwHighDateTime;
646-
}
647-
let large = large.assume_init();
648-
*large.QuadPart()
649-
}
639+
let large: [u32; 2] = [time.dwLowDateTime, time.dwHighDateTime];
640+
unsafe { std::mem::transmute(large) }
650641
}
651642

652643
fn win_perf_counter_frequency(vm: &VirtualMachine) -> PyResult<i64> {
653-
let freq = unsafe {
644+
let frequency = unsafe {
654645
let mut freq = std::mem::MaybeUninit::uninit();
655646
if QueryPerformanceFrequency(freq.as_mut_ptr()) == 0 {
656647
return Err(errno_err(vm));
657648
}
658649
freq.assume_init()
659650
};
660-
let frequency = unsafe { freq.QuadPart() };
661651

662-
if *frequency < 1 {
652+
if frequency < 1 {
663653
Err(vm.new_runtime_error("invalid QueryPerformanceFrequency".to_owned()))
664-
} else if *frequency > i64::MAX / SEC_TO_NS {
654+
} else if frequency > i64::MAX / SEC_TO_NS {
665655
Err(vm.new_overflow_error("QueryPerformanceFrequency is too large".to_owned()))
666656
} else {
667-
Ok(*frequency)
657+
Ok(frequency)
668658
}
669659
}
670660

@@ -678,15 +668,14 @@ mod platform {
678668
}
679669

680670
pub(super) fn get_perf_time(vm: &VirtualMachine) -> PyResult<Duration> {
681-
let now = unsafe {
671+
let ticks = unsafe {
682672
let mut performance_count = std::mem::MaybeUninit::uninit();
683673
QueryPerformanceCounter(performance_count.as_mut_ptr());
684674
performance_count.assume_init()
685675
};
686676

687-
let ticks = unsafe { now.QuadPart() };
688677
Ok(Duration::from_nanos(time_muldiv(
689-
*ticks,
678+
ticks,
690679
SEC_TO_NS,
691680
global_frequency(vm)?,
692681
)))

‎vm/src/stdlib/winreg.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ mod winreg {
3434
};
3535
use ::winreg::{enums::RegType, RegKey, RegValue};
3636
use std::{ffi::OsStr, io};
37-
use winapi::shared::winerror;
37+
use windows_sys::Win32::Foundation;
3838

3939
// access rights
4040
#[pyattr]
@@ -201,7 +201,7 @@ mod winreg {
201201
key.with_key(|k| k.enum_keys().nth(index as usize))
202202
.unwrap_or_else(|| {
203203
Err(io::Error::from_raw_os_error(
204-
winerror::ERROR_NO_MORE_ITEMS as i32,
204+
Foundation::ERROR_NO_MORE_ITEMS as i32,
205205
))
206206
})
207207
.map_err(|e| e.to_pyexception(vm))
@@ -217,7 +217,7 @@ mod winreg {
217217
.with_key(|k| k.enum_values().nth(index as usize))
218218
.unwrap_or_else(|| {
219219
Err(io::Error::from_raw_os_error(
220-
winerror::ERROR_NO_MORE_ITEMS as i32,
220+
Foundation::ERROR_NO_MORE_ITEMS as i32,
221221
))
222222
})
223223
.map_err(|e| e.to_pyexception(vm))?;

0 commit comments

Comments
 (0)