Visitar URL original
more hostenv isolation by youknowone · Pull Request #7886 · RustPython/RustPython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a5ac299
Convert host_env Windows path/argv params from raw *const u16 to &Wid…
youknowone May 16, 2026
cea48cc
Migrate remaining winapi raw u16 pointer signatures to typed references
youknowone May 16, 2026
25a9d40
Migrate winreg pub unsafe fn string parameters to typed references
youknowone May 16, 2026
0acdcad
Add ToPyException impls for host_env error types (PyPy wrap_oserror a…
youknowone May 16, 2026
dc57418
Add CheckLibcResult helper and apply to socket/fcntl/shm/posix_wasi
youknowone May 16, 2026
2d4be2c
Add Win32 BOOL/HANDLE check helpers; apply check helpers across host_env
youknowone May 16, 2026
592e8b4
Apply Win32/libc check helpers to overlapped/testconsole/os.rs
youknowone May 16, 2026
86055d2
Apply Win32 check helpers to winapi.rs (partial)
youknowone May 16, 2026
fe71c58
Apply Win32 check helpers across more winapi.rs functions
youknowone May 16, 2026
a7c567a
Apply Win32 check helpers to nt.rs (partial)
youknowone May 16, 2026
5329cc5
Add CheckWin32Sentinel helper; apply to nt.rs INVALID_HANDLE_VALUE/IN…
youknowone May 16, 2026
f1532e2
Add OwnedHandle / HandleToOwned helper; apply to mmap create_named_ma…
youknowone May 16, 2026
e768278
Use OwnedHandle RAII in nt::pipe to eliminate manual cleanup on error…
youknowone May 16, 2026
b730f7e
Use OwnedHandle in nt::chmod_follow; hoist HandleToOwned import
youknowone May 16, 2026
67ece5b
Drop rustix dependency from vm crate
youknowone May 17, 2026
a61d9bd
Fix CI failures: cross-platform regressions
youknowone May 19, 2026
3320c2a
Fix CI failures: cfg gates and unused imports
youknowone May 19, 2026
e9ec6e8
Fix CI failures: rustfmt and windows unused import
youknowone May 19, 2026
ba3f4fa
Push remaining libc/extern callsites from vm into host_env
youknowone May 19, 2026
45c83dd
Move time tz state and winsound FFI into host_env
youknowone May 19, 2026
49c13f8
Migrate unsetenv to host_env::nt::wputenv; rustfmt
youknowone May 20, 2026
61e0fd4
Address PR review comments
youknowone May 20, 2026
783c6b5
Fix CI failures and address review follow-ups
youknowone May 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate remaining winapi raw u16 pointer signatures to typed references
  • Loading branch information
youknowone committed May 20, 2026
commit cea48cc9e2c4286b98b421b4d4e06e5bd756925d
74 changes: 38 additions & 36 deletions crates/host_env/src/winapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ impl Drop for AttrList {
}

pub fn create_file_w(
file_name: *const u16,
file_name: &widestring::WideCStr,
desired_access: u32,
share_mode: u32,
creation_disposition: u32,
flags_and_attributes: u32,
) -> io::Result<HANDLE> {
let handle = unsafe {
windows_sys::Win32::Storage::FileSystem::CreateFileW(
file_name,
file_name.as_ptr(),
desired_access,
share_mode,
core::ptr::null(),
Expand All @@ -188,27 +188,27 @@ pub fn create_file_w(
}

/// # Safety
/// The pointer arguments must follow the Win32 `CreateProcessW` contract.
pub unsafe fn create_process_w(
app_name: *const u16,
command_line: *mut u16,
/// `startup_info` must point to a valid `STARTUPINFOW` (or extended).
unsafe fn create_process_w_raw(
app_name: Option<&widestring::WideCStr>,
command_line: Option<&mut [u16]>,
inherit_handles: i32,
creation_flags: u32,
env: *mut u16,
current_dir: *mut u16,
env: Option<&[u16]>,
current_dir: Option<&widestring::WideCStr>,
startup_info: *mut windows_sys::Win32::System::Threading::STARTUPINFOW,
) -> io::Result<PROCESS_INFORMATION> {
let mut procinfo = core::mem::MaybeUninit::<PROCESS_INFORMATION>::uninit();
let ok = unsafe {
windows_sys::Win32::System::Threading::CreateProcessW(
app_name,
command_line,
app_name.map_or(core::ptr::null(), |s| s.as_ptr()),
command_line.map_or(core::ptr::null_mut(), |s| s.as_mut_ptr()),
core::ptr::null(),
core::ptr::null(),
inherit_handles,
creation_flags,
env.cast(),
current_dir,
env.map_or(core::ptr::null(), |s| s.as_ptr().cast()),
current_dir.map_or(core::ptr::null(), |s| s.as_ptr()),
startup_info,
procinfo.as_mut_ptr(),
)
Expand All @@ -225,12 +225,12 @@ pub unsafe fn create_process_w(
reason = "This is the semantic host wrapper for Win32 CreateProcess parameters."
)]
pub fn create_process(
app_name: *const u16,
command_line: *mut u16,
app_name: Option<&widestring::WideCStr>,
command_line: Option<&mut [u16]>,
inherit_handles: i32,
creation_flags: u32,
env: *mut u16,
current_dir: *mut u16,
env: Option<&[u16]>,
current_dir: Option<&widestring::WideCStr>,
startup_info: StartupInfoData,
handle_list: Option<Vec<usize>>,
) -> io::Result<ProcessInfo> {
Expand All @@ -249,7 +249,7 @@ pub fn create_process(
.map_or_else(core::ptr::null_mut, |l| l.as_mut_ptr() as _);

let procinfo = unsafe {
create_process_w(
create_process_w_raw(
app_name,
command_line,
inherit_handles,
Expand Down Expand Up @@ -416,14 +416,15 @@ pub fn create_pipe(size: u32) -> io::Result<(HANDLE, HANDLE)> {
pub fn create_event_w(
manual_reset: bool,
initial_state: bool,
name: *const u16,
name: Option<&widestring::WideCStr>,
) -> io::Result<HANDLE> {
let name_ptr = name.map_or(core::ptr::null(), |n| n.as_ptr());
let handle = unsafe {
windows_sys::Win32::System::Threading::CreateEventW(
core::ptr::null(),
i32::from(manual_reset),
i32::from(initial_state),
name,
name_ptr,
)
};
if handle.is_null() {
Expand Down Expand Up @@ -897,13 +898,13 @@ fn get_path_name_impl(
pub fn open_mutex_w(
desired_access: u32,
inherit_handle: bool,
name: *const u16,
name: &widestring::WideCStr,
) -> io::Result<HANDLE> {
let handle = unsafe {
windows_sys::Win32::System::Threading::OpenMutexW(
desired_access,
i32::from(inherit_handle),
name,
name.as_ptr(),
)
};
if handle.is_null() {
Expand All @@ -918,7 +919,7 @@ pub fn release_mutex(handle: HANDLE) -> i32 {
}

pub fn create_named_pipe_w(
name: *const u16,
name: &widestring::WideCStr,
open_mode: u32,
pipe_mode: u32,
max_instances: u32,
Expand All @@ -928,7 +929,7 @@ pub fn create_named_pipe_w(
) -> io::Result<HANDLE> {
let handle = unsafe {
windows_sys::Win32::System::Pipes::CreateNamedPipeW(
name,
name.as_ptr(),
open_mode,
pipe_mode,
max_instances,
Expand All @@ -950,16 +951,17 @@ pub fn create_file_mapping_w(
protect: u32,
max_size_high: u32,
max_size_low: u32,
name: *const u16,
name: Option<&widestring::WideCStr>,
) -> io::Result<HANDLE> {
let name_ptr = name.map_or(core::ptr::null(), |n| n.as_ptr());
let handle = unsafe {
windows_sys::Win32::System::Memory::CreateFileMappingW(
file_handle,
core::ptr::null(),
protect,
max_size_high,
max_size_low,
name,
name_ptr,
)
};
if handle.is_null() {
Expand All @@ -972,13 +974,13 @@ pub fn create_file_mapping_w(
pub fn open_file_mapping_w(
desired_access: u32,
inherit_handle: bool,
name: *const u16,
name: &widestring::WideCStr,
) -> io::Result<HANDLE> {
let handle = unsafe {
windows_sys::Win32::System::Memory::OpenFileMappingW(
desired_access,
i32::from(inherit_handle),
name,
name.as_ptr(),
)
};
if handle.is_null() {
Expand Down Expand Up @@ -1168,16 +1170,16 @@ where
}

pub fn lc_map_string_ex(
locale: *const u16,
locale: &widestring::WideCStr,
flags: u32,
src: *const u16,
src_len: i32,
src: &[u16],
) -> io::Result<Vec<u16>> {
let src_len = src.len() as i32;
let dest_size = unsafe {
windows_sys::Win32::Globalization::LCMapStringEx(
locale,
locale.as_ptr(),
flags,
src,
src.as_ptr(),
src_len,
core::ptr::null_mut(),
0,
Expand All @@ -1193,9 +1195,9 @@ pub fn lc_map_string_ex(
let mut dest = vec![0u16; dest_size as usize];
let nmapped = unsafe {
windows_sys::Win32::Globalization::LCMapStringEx(
locale,
locale.as_ptr(),
flags,
src,
src.as_ptr(),
src_len,
dest.as_mut_ptr(),
dest_size,
Expand Down Expand Up @@ -1388,13 +1390,13 @@ pub fn create_mutex_w(
pub fn open_event_w(
desired_access: u32,
inherit_handle: bool,
name: *const u16,
name: &widestring::WideCStr,
) -> io::Result<HANDLE> {
let handle = unsafe {
windows_sys::Win32::System::Threading::OpenEventW(
desired_access,
i32::from(inherit_handle),
name,
name.as_ptr(),
)
};
if handle.is_null() {
Expand Down
3 changes: 3 additions & 0 deletions crates/host_env/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ pub fn multi_byte_to_wide(
pub trait ToWideString {
fn to_wide(&self) -> Vec<u16>;
fn to_wide_with_nul(&self) -> Vec<u16>;
fn to_wide_cstring(&self) -> widestring::WideCString {
widestring::WideCString::from_vec_truncate(self.to_wide())
}
}

impl<T> ToWideString for T
Expand Down
16 changes: 6 additions & 10 deletions crates/stdlib/src/overlapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ mod _overlapped {
let mut event = event.unwrap_or(INVALID_HANDLE_VALUE);

if event == INVALID_HANDLE_VALUE {
event = host_winapi::create_event_w(true, false, core::ptr::null())
event = host_winapi::create_event_w(true, false, None)
.map(|handle| handle as isize)
.map_err(|err| {
set_from_windows_err(err.raw_os_error().unwrap_or(0) as u32, vm)
Expand Down Expand Up @@ -1251,15 +1251,11 @@ mod _overlapped {
return Err(vm.new_value_error("EventAttributes must be None"));
}

let name_wide: Option<Vec<u16>> =
name.map(|n| n.encode_utf16().chain(core::iter::once(0)).collect());
host_winapi::create_event_w(
manual_reset,
initial_state,
name_wide.as_ref().map_or(core::ptr::null(), |n| n.as_ptr()),
)
.map(|h| h as isize)
.map_err(|err| set_from_windows_err(err.raw_os_error().unwrap_or(0) as u32, vm))
let name_wide: Option<widestring::WideCString> =
name.map(|n| widestring::WideCString::from_str_truncate(&n));
host_winapi::create_event_w(manual_reset, initial_state, name_wide.as_deref())
.map(|h| h as isize)
.map_err(|err| set_from_windows_err(err.raw_os_error().unwrap_or(0) as u32, vm))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}

#[pyfunction]
Expand Down
5 changes: 2 additions & 3 deletions crates/vm/src/stdlib/_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5843,6 +5843,7 @@ mod winconsoleio {
use crossbeam_utils::atomic::AtomicCell;
use rustpython_host_env::io as host_io;
use rustpython_host_env::nt as host_nt;
use rustpython_host_env::windows::ToWideString;
type HANDLE = host_nt::Handle;

const SMALLBUF: usize = 4;
Expand Down Expand Up @@ -6013,9 +6014,7 @@ mod winconsoleio {
}

let name_str = nameobj.str(vm)?;
let wide = widestring::WideCString::from_vec_truncate(
name_str.as_wtf8().encode_wide().collect::<Vec<u16>>(),
);
let wide = name_str.as_wtf8().to_wide_cstring();

fd = host_nt::open_console_path_fd(&wide, writable)
.map_err(|err| err.to_pyexception(vm))?;
Expand Down
Loading