Skip to content

Commit 6df9732

Browse files
committed
replace wincon to windows-sys
1 parent 8a84a47 commit 6df9732

3 files changed

Lines changed: 35 additions & 36 deletions

File tree

‎vm/Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ features = [
141141
[target.'cfg(windows)'.dependencies.winapi]
142142
version = "0.3.9"
143143
features = [
144-
"winsock2", "handleapi", "std", "winbase", "processenv",
145-
"winnt", "errhandlingapi", "wincon",
144+
"winsock2", "handleapi", "std", "winbase",
145+
"winnt", "errhandlingapi",
146146
"impl-default", "vcruntime",
147147
]
148148

‎vm/src/stdlib/nt.rs‎

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
1010

1111
#[pymodule(name = "nt", with(super::os::_os))]
1212
pub(crate) mod module {
13+
#[cfg(target_env = "msvc")]
14+
use crate::builtins::PyListRef;
1315
use crate::{
14-
builtins::{PyStrRef, PyTupleRef},
16+
builtins::{PyDictRef, PyStrRef, PyTupleRef},
1517
common::{crt_fd::Fd, os::errno, suppress_iph},
1618
convert::ToPyException,
1719
function::Either,
@@ -23,13 +25,14 @@ pub(crate) mod module {
2325
};
2426
use std::{
2527
env, fs, io,
28+
mem::MaybeUninit,
2629
os::windows::ffi::{OsStrExt, OsStringExt},
2730
};
28-
29-
use crate::builtins::PyDictRef;
30-
#[cfg(target_env = "msvc")]
31-
use crate::builtins::PyListRef;
3231
use winapi::{um, vc::vcruntime::intptr_t};
32+
use windows_sys::Win32::{
33+
Foundation::{CloseHandle, INVALID_HANDLE_VALUE},
34+
System::{Console, Threading},
35+
};
3336

3437
#[pyattr]
3538
use libc::{O_BINARY, O_TEMPORARY};
@@ -135,27 +138,23 @@ pub(crate) mod module {
135138

136139
#[pyfunction]
137140
fn kill(pid: i32, sig: isize, vm: &VirtualMachine) -> PyResult<()> {
138-
{
139-
use um::{wincon, winnt};
140-
use windows_sys::Win32::{Foundation::CloseHandle, System::Threading};
141-
let sig = sig as u32;
142-
let pid = pid as u32;
143-
144-
if sig == wincon::CTRL_C_EVENT || sig == wincon::CTRL_BREAK_EVENT {
145-
let ret = unsafe { wincon::GenerateConsoleCtrlEvent(sig, pid) };
146-
let res = if ret == 0 { Err(errno_err(vm)) } else { Ok(()) };
147-
return res;
148-
}
141+
let sig = sig as u32;
142+
let pid = pid as u32;
149143

150-
let h = unsafe { Threading::OpenProcess(winnt::PROCESS_ALL_ACCESS, 0, pid) };
151-
if h == 0 {
152-
return Err(errno_err(vm));
153-
}
154-
let ret = unsafe { Threading::TerminateProcess(h, sig) };
144+
if sig == Console::CTRL_C_EVENT || sig == Console::CTRL_BREAK_EVENT {
145+
let ret = unsafe { Console::GenerateConsoleCtrlEvent(sig, pid) };
155146
let res = if ret == 0 { Err(errno_err(vm)) } else { Ok(()) };
156-
unsafe { CloseHandle(h) };
157-
res
147+
return res;
158148
}
149+
150+
let h = unsafe { Threading::OpenProcess(Threading::PROCESS_ALL_ACCESS, 0, pid) };
151+
if h == 0 {
152+
return Err(errno_err(vm));
153+
}
154+
let ret = unsafe { Threading::TerminateProcess(h, sig) };
155+
let res = if ret == 0 { Err(errno_err(vm)) } else { Ok(()) };
156+
unsafe { CloseHandle(h) };
157+
res
159158
}
160159

161160
#[pyfunction]
@@ -164,22 +163,22 @@ pub(crate) mod module {
164163
vm: &VirtualMachine,
165164
) -> PyResult<_os::PyTerminalSize> {
166165
let (columns, lines) = {
167-
use um::{handleapi, processenv, winbase, wincon};
168166
let stdhandle = match fd {
169-
OptionalArg::Present(0) => winbase::STD_INPUT_HANDLE,
170-
OptionalArg::Present(1) | OptionalArg::Missing => winbase::STD_OUTPUT_HANDLE,
171-
OptionalArg::Present(2) => winbase::STD_ERROR_HANDLE,
167+
OptionalArg::Present(0) => Console::STD_INPUT_HANDLE,
168+
OptionalArg::Present(1) | OptionalArg::Missing => Console::STD_OUTPUT_HANDLE,
169+
OptionalArg::Present(2) => Console::STD_ERROR_HANDLE,
172170
_ => return Err(vm.new_value_error("bad file descriptor".to_owned())),
173171
};
174-
let h = unsafe { processenv::GetStdHandle(stdhandle) };
175-
if h.is_null() {
172+
let h = unsafe { Console::GetStdHandle(stdhandle) };
173+
if h == 0 {
176174
return Err(vm.new_os_error("handle cannot be retrieved".to_owned()));
177175
}
178-
if h == handleapi::INVALID_HANDLE_VALUE {
176+
if h == INVALID_HANDLE_VALUE {
179177
return Err(errno_err(vm));
180178
}
181-
let mut csbi = wincon::CONSOLE_SCREEN_BUFFER_INFO::default();
182-
let ret = unsafe { wincon::GetConsoleScreenBufferInfo(h, &mut csbi) };
179+
let mut csbi = MaybeUninit::uninit();
180+
let ret = unsafe { Console::GetConsoleScreenBufferInfo(h, csbi.as_mut_ptr()) };
181+
let csbi = unsafe { csbi.assume_init() };
183182
if ret == 0 {
184183
return Err(errno_err(vm));
185184
}

‎vm/src/windows.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
PyObjectRef, PyResult, TryFromObject, VirtualMachine,
55
};
66
use windows::Win32::Foundation::HANDLE;
7-
use windows_sys::Win32::Foundation::{BOOL, HANDLE as RAW_HANDLE};
7+
use windows_sys::Win32::Foundation::{BOOL, HANDLE as RAW_HANDLE, INVALID_HANDLE_VALUE};
88

99
pub(crate) trait WindowsSysResultValue {
1010
type Ok: ToPyObject;
@@ -15,7 +15,7 @@ pub(crate) trait WindowsSysResultValue {
1515
impl WindowsSysResultValue for RAW_HANDLE {
1616
type Ok = HANDLE;
1717
fn is_err(&self) -> bool {
18-
*self == windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE
18+
*self == INVALID_HANDLE_VALUE
1919
}
2020
fn into_ok(self) -> Self::Ok {
2121
HANDLE(self)

0 commit comments

Comments
 (0)