Visitar URL original
Build host_env on Windows without the ctypes feature (#9000) · RustPython/RustPython@a38bf3b · GitHub
Skip to content

Commit a38bf3b

Browse files
authored
Build host_env on Windows without the ctypes feature (#9000)
Assisted-by: Claude Code:claude-opus-5-5 Assisted-by: Codex:gpt-6
1 parent 95686c0 commit a38bf3b

4 files changed

Lines changed: 38 additions & 34 deletions

File tree

‎.github/workflows/ci.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ jobs:
295295
working-directory: crates/capi
296296
run: cargo test --doc --locked
297297

298+
- name: Check host_env without ctypes
299+
if: env.RUN_RUST_TESTS == 'true' && runner.os == 'Windows'
300+
run: cargo check --locked -p rustpython-host_env --no-default-features --lib
301+
298302
- name: check compilation without host_env (sandbox mode)
299303
if: env.RUN_RUST_TESTS == 'true' && runner.os == 'Linux'
300304
run: |

‎crates/host_env/src/ctypes.rs‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,29 +2809,7 @@ pub fn free_library(module: windows_sys::Win32::Foundation::HMODULE) -> std::io:
28092809
}
28102810

28112811
#[cfg(windows)]
2812-
#[allow(clippy::not_unsafe_ptr_arg_deref)]
2813-
pub fn sys_string_len(bstr: *const u16) -> usize {
2814-
unsafe { windows_sys::Win32::Foundation::SysStringLen(bstr) as usize }
2815-
}
2816-
2817-
#[cfg(windows)]
2818-
#[allow(clippy::not_unsafe_ptr_arg_deref)]
2819-
pub fn sys_free_string(bstr: *const u16) {
2820-
unsafe { windows_sys::Win32::Foundation::SysFreeString(bstr) };
2821-
}
2822-
2823-
#[cfg(windows)]
2824-
pub fn sys_alloc_string_len(units: &[u16]) -> Option<*mut u16> {
2825-
let len = u32::try_from(units.len()).ok()?;
2826-
let bstr = unsafe { windows_sys::Win32::Foundation::SysAllocStringLen(units.as_ptr(), len) };
2827-
(!bstr.is_null()).then_some(bstr as *mut u16)
2828-
}
2829-
2830-
#[cfg(windows)]
2831-
#[allow(clippy::not_unsafe_ptr_arg_deref)]
2832-
pub fn co_task_mem_free(ptr: *mut core::ffi::c_void) {
2833-
unsafe { windows_sys::Win32::System::Com::CoTaskMemFree(ptr) };
2834-
}
2812+
pub use crate::windows::{co_task_mem_free, sys_alloc_string_len, sys_free_string, sys_string_len};
28352813

28362814
/// `GetErrorInfo(0)`. `None` when the thread has no error object (`S_FALSE`).
28372815
#[cfg(windows)]

‎crates/host_env/src/windows.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,24 @@ impl ToWideString for Wtf8 {
426426
WideCString::from_vec(buf).map_err(|_| io::Error::other(NUL_ERROR))
427427
}
428428
}
429+
430+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
431+
pub fn sys_string_len(bstr: *const u16) -> usize {
432+
unsafe { windows_sys::Win32::Foundation::SysStringLen(bstr) as usize }
433+
}
434+
435+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
436+
pub fn sys_free_string(bstr: *const u16) {
437+
unsafe { windows_sys::Win32::Foundation::SysFreeString(bstr) };
438+
}
439+
440+
pub fn sys_alloc_string_len(units: &[u16]) -> Option<*mut u16> {
441+
let len = u32::try_from(units.len()).ok()?;
442+
let bstr = unsafe { windows_sys::Win32::Foundation::SysAllocStringLen(units.as_ptr(), len) };
443+
(!bstr.is_null()).then_some(bstr as *mut u16)
444+
}
445+
446+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
447+
pub fn co_task_mem_free(ptr: *mut core::ffi::c_void) {
448+
unsafe { windows_sys::Win32::System::Com::CoTaskMemFree(ptr) };
449+
}

‎crates/host_env/src/wmi.rs‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use core::ffi::c_void;
99
use core::ptr::{NonNull, null, null_mut};
10-
use widestring::WideCString;
10+
use widestring::{WideCStr, WideCString};
1111
use windows_sys::Win32::Foundation::{
1212
CloseHandle, ERROR_BROKEN_PIPE, ERROR_INVALID_NAME, ERROR_MORE_DATA, ERROR_NOT_ENOUGH_MEMORY,
1313
GetLastError, HANDLE, WAIT_OBJECT_0, WAIT_TIMEOUT,
@@ -19,7 +19,7 @@ use windows_sys::Win32::System::Threading::{
1919
};
2020
use windows_sys::w;
2121

22-
use crate::ctypes::wcslen;
22+
use crate::windows::{sys_alloc_string_len, sys_free_string};
2323

2424
pub const BUFFER_SIZE: usize = 8192;
2525

@@ -122,21 +122,20 @@ fn alloc_bstr(wide: &[u16]) -> *mut u16 {
122122
.iter()
123123
.position(|&unit| unit == 0)
124124
.unwrap_or(wide.len());
125-
crate::ctypes::sys_alloc_string_len(&wide[..len]).unwrap_or(null_mut())
125+
sys_alloc_string_len(&wide[..len]).unwrap_or(null_mut())
126126
}
127127

128128
fn alloc_bstr_ptr(ptr: *const u16) -> *mut u16 {
129129
let Some(ptr) = NonNull::new(ptr.cast_mut()) else {
130130
return null_mut();
131131
};
132-
let len = unsafe { crate::ctypes::wcslen(ptr) };
133-
crate::ctypes::sys_alloc_string_len(unsafe { core::slice::from_raw_parts(ptr.as_ptr(), len) })
134-
.unwrap_or(null_mut())
132+
let wide = unsafe { WideCStr::from_ptr_str(ptr.as_ptr()) };
133+
sys_alloc_string_len(wide.as_slice()).unwrap_or(null_mut())
135134
}
136135

137136
fn free_bstr(bstr: *mut u16) {
138137
if !bstr.is_null() {
139-
crate::ctypes::sys_free_string(bstr);
138+
sys_free_string(bstr);
140139
}
141140
}
142141

@@ -478,9 +477,9 @@ unsafe fn query_thread_impl(param: *mut c_void) -> u32 {
478477
}
479478

480479
if succeeded(hr) && (flavor & WBEM_FLAVOR_MASK_ORIGIN) != WBEM_FLAVOR_ORIGIN_SYSTEM {
481-
let Some(cb_str1) = NonNull::new(prop_name)
482-
.map(|prop_name| (unsafe { wcslen(prop_name) } * 2) as u32)
483-
else {
480+
let Some(cb_str1) = NonNull::new(prop_name).map(|prop_name| {
481+
(unsafe { WideCStr::from_ptr_str(prop_name.as_ptr()) }.len() * 2) as u32
482+
}) else {
484483
free_bstr(prop_name);
485484
break;
486485
};
@@ -490,7 +489,9 @@ unsafe fn query_thread_impl(param: *mut c_void) -> u32 {
490489
VariantToString(&prop_value, prop_str.as_mut_ptr(), BUFFER_SIZE as u32)
491490
};
492491
let cb_str2 = NonNull::new(prop_str.as_ptr().cast_mut())
493-
.map(|prop_str| (unsafe { wcslen(prop_str) } * 2) as u32)
492+
.map(|prop_str| {
493+
(unsafe { WideCStr::from_ptr_str(prop_str.as_ptr()) }.len() * 2) as u32
494+
})
494495
.expect("prop_str is never null");
495496

496497
if succeeded(hr)

0 commit comments

Comments
 (0)