Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
use ToWide
  • Loading branch information
youknowone committed Dec 8, 2025
commit 660b9e5177daa24f9807a19779ef4860834eee88
4 changes: 1 addition & 3 deletions crates/common/src/fileutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub mod windows {
use crate::windows::ToWideString;
use libc::{S_IFCHR, S_IFDIR, S_IFMT};
use std::ffi::{CString, OsStr, OsString};
use std::os::windows::ffi::OsStrExt;
use std::os::windows::io::AsRawHandle;
use std::sync::OnceLock;
use windows_sys::Win32::Foundation::{
Expand Down Expand Up @@ -75,8 +74,7 @@ pub mod windows {
pub fn update_st_mode_from_path(&mut self, path: &OsStr, attr: u32) {
if attr & FILE_ATTRIBUTE_DIRECTORY == 0 {
let file_extension = path
.encode_wide()
.collect::<Vec<u16>>()
.to_wide()
.split(|&c| c == '.' as u16)
.next_back()
.and_then(|s| String::from_utf16(s).ok());
Expand Down
8 changes: 4 additions & 4 deletions crates/vm/src/stdlib/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ mod _codecs {
#[cfg(windows)]
#[pyfunction]
fn mbcs_encode(args: MbcsEncodeArgs, vm: &VirtualMachine) -> PyResult<(Vec<u8>, usize)> {
use std::os::windows::ffi::OsStrExt;
use crate::common::windows::ToWideString;
use windows_sys::Win32::Globalization::{
CP_ACP, WC_NO_BEST_FIT_CHARS, WideCharToMultiByte,
};
Expand All @@ -259,7 +259,7 @@ mod _codecs {
}

// Convert UTF-8 string to UTF-16
let wide: Vec<u16> = std::ffi::OsStr::new(s).encode_wide().collect();
let wide: Vec<u16> = std::ffi::OsStr::new(s).to_wide();

// Get the required buffer size
let size = unsafe {
Expand Down Expand Up @@ -439,7 +439,7 @@ mod _codecs {
#[cfg(windows)]
#[pyfunction]
fn oem_encode(args: OemEncodeArgs, vm: &VirtualMachine) -> PyResult<(Vec<u8>, usize)> {
use std::os::windows::ffi::OsStrExt;
use crate::common::windows::ToWideString;
use windows_sys::Win32::Globalization::{
CP_OEMCP, WC_NO_BEST_FIT_CHARS, WideCharToMultiByte,
};
Expand All @@ -461,7 +461,7 @@ mod _codecs {
}

// Convert UTF-8 string to UTF-16
let wide: Vec<u16> = std::ffi::OsStr::new(s).encode_wide().collect();
let wide: Vec<u16> = std::ffi::OsStr::new(s).to_wide();

// Get the required buffer size
let size = unsafe {
Expand Down
10 changes: 3 additions & 7 deletions crates/vm/src/stdlib/nt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ pub(crate) mod module {
use crate::{
PyResult, TryFromObject, VirtualMachine,
builtins::{PyBaseExceptionRef, PyDictRef, PyListRef, PyStrRef, PyTupleRef},
common::{crt_fd, os::last_os_error, suppress_iph},
common::{crt_fd, os::last_os_error, suppress_iph, windows::ToWideString},
convert::ToPyException,
function::{Either, OptionalArg},
ospath::OsPath,
stdlib::os::{_os, DirFd, FollowSymlinks, SupportFunc, TargetIsDirectory, errno_err},
};
use libc::intptr_t;
use std::os::windows::io::AsRawHandle;
use std::{
env, fs, io,
mem::MaybeUninit,
os::windows::ffi::{OsStrExt, OsStringExt},
};
use std::{env, fs, io, mem::MaybeUninit, os::windows::ffi::OsStringExt};
use windows_sys::Win32::{
Foundation::{self, INVALID_HANDLE_VALUE},
Storage::FileSystem,
Expand Down Expand Up @@ -541,7 +537,7 @@ pub(crate) mod module {

#[pyfunction]
fn _path_splitroot(path: OsPath, vm: &VirtualMachine) -> PyResult<(String, String)> {
let orig: Vec<_> = path.path.encode_wide().collect();
let orig: Vec<_> = path.path.to_wide();
if orig.is_empty() {
return Ok(("".to_owned(), "".to_owned()));
}
Expand Down
13 changes: 3 additions & 10 deletions crates/vm/src/stdlib/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod sys {
common::{
ascii,
hash::{PyHash, PyUHash},
windows::ToWideString,
},
convert::ToPyObject,
frame::FrameRef,
Expand All @@ -22,8 +23,6 @@ mod sys {
vm::{Settings, VirtualMachine},
};
use num_traits::ToPrimitive;
#[cfg(windows)]
use std::os::windows::ffi::OsStrExt;
use std::{
env::{self, VarError},
io::Read,
Expand Down Expand Up @@ -553,10 +552,7 @@ mod sys {
fn get_kernel32_version() -> std::io::Result<(u32, u32, u32)> {
unsafe {
// Create a wide string for "kernel32.dll"
let module_name: Vec<u16> = std::ffi::OsStr::new("kernel32.dll")
.encode_wide()
.chain(Some(0))
.collect();
let module_name: Vec<u16> = std::ffi::OsStr::new("kernel32.dll").to_wide_with_nul();
let h_kernel32 = GetModuleHandleW(module_name.as_ptr());
if h_kernel32.is_null() {
return Err(std::io::Error::last_os_error());
Expand Down Expand Up @@ -593,10 +589,7 @@ mod sys {
}

// Prepare an empty sub-block string (L"") as required by VerQueryValueW
let sub_block: Vec<u16> = std::ffi::OsStr::new("")
.encode_wide()
.chain(Some(0))
.collect();
let sub_block: Vec<u16> = std::ffi::OsStr::new("").to_wide_with_nul();

let mut ffi_ptr: *mut VS_FIXEDFILEINFO = std::ptr::null_mut();
let mut ffi_len: u32 = 0;
Expand Down
7 changes: 3 additions & 4 deletions crates/vm/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
convert::{ToPyObject, ToPyResult},
stdlib::os::errno_err,
};
use rustpython_common::windows::ToWideString;
use std::ffi::OsStr;
use windows::Win32::Foundation::HANDLE;
use windows_sys::Win32::Foundation::{HANDLE as RAW_HANDLE, INVALID_HANDLE_VALUE};
Expand Down Expand Up @@ -235,13 +236,12 @@ fn attributes_from_dir(
windows_sys::Win32::Storage::FileSystem::BY_HANDLE_FILE_INFORMATION,
u32,
)> {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::Storage::FileSystem::{
BY_HANDLE_FILE_INFORMATION, FILE_ATTRIBUTE_REPARSE_POINT, FindClose, FindFirstFileW,
WIN32_FIND_DATAW,
};

let wide: Vec<u16> = path.encode_wide().chain(std::iter::once(0)).collect();
let wide: Vec<u16> = path.to_wide_with_nul();
let mut find_data: WIN32_FIND_DATAW = unsafe { std::mem::zeroed() };

let handle = unsafe { FindFirstFileW(wide.as_ptr(), &mut find_data) };
Expand Down Expand Up @@ -269,7 +269,6 @@ fn attributes_from_dir(

/// Ported from win32_xstat_slow_impl
fn win32_xstat_slow_impl(path: &OsStr, traverse: bool) -> std::io::Result<StatStruct> {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::{
Foundation::{
CloseHandle, ERROR_ACCESS_DENIED, ERROR_CANT_ACCESS_FILE, ERROR_INVALID_FUNCTION,
Expand All @@ -287,7 +286,7 @@ fn win32_xstat_slow_impl(path: &OsStr, traverse: bool) -> std::io::Result<StatSt
},
};

let wide: Vec<u16> = path.encode_wide().chain(std::iter::once(0)).collect();
let wide: Vec<u16> = path.to_wide_with_nul();

let access = FILE_READ_ATTRIBUTES;
let mut flags = FILE_FLAG_BACKUP_SEMANTICS;
Expand Down