Skip to content

Commit 2ffb484

Browse files
committed
use ToWide
1 parent bf565e9 commit 2ffb484

File tree

5 files changed

+14
-28
lines changed

5 files changed

+14
-28
lines changed

crates/common/src/fileutils.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub mod windows {
2626
use crate::windows::ToWideString;
2727
use libc::{S_IFCHR, S_IFDIR, S_IFMT};
2828
use std::ffi::{CString, OsStr, OsString};
29-
use std::os::windows::ffi::OsStrExt;
3029
use std::os::windows::io::AsRawHandle;
3130
use std::sync::OnceLock;
3231
use windows_sys::Win32::Foundation::{
@@ -75,8 +74,7 @@ pub mod windows {
7574
pub fn update_st_mode_from_path(&mut self, path: &OsStr, attr: u32) {
7675
if attr & FILE_ATTRIBUTE_DIRECTORY == 0 {
7776
let file_extension = path
78-
.encode_wide()
79-
.collect::<Vec<u16>>()
77+
.to_wide()
8078
.split(|&c| c == '.' as u16)
8179
.next_back()
8280
.and_then(|s| String::from_utf16(s).ok());

crates/vm/src/stdlib/codecs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ mod _codecs {
237237
#[cfg(windows)]
238238
#[pyfunction]
239239
fn mbcs_encode(args: MbcsEncodeArgs, vm: &VirtualMachine) -> PyResult<(Vec<u8>, usize)> {
240-
use std::os::windows::ffi::OsStrExt;
240+
use crate::common::windows::ToWideString;
241241
use windows_sys::Win32::Globalization::{
242242
CP_ACP, WC_NO_BEST_FIT_CHARS, WideCharToMultiByte,
243243
};
@@ -259,7 +259,7 @@ mod _codecs {
259259
}
260260

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

264264
// Get the required buffer size
265265
let size = unsafe {
@@ -439,7 +439,7 @@ mod _codecs {
439439
#[cfg(windows)]
440440
#[pyfunction]
441441
fn oem_encode(args: OemEncodeArgs, vm: &VirtualMachine) -> PyResult<(Vec<u8>, usize)> {
442-
use std::os::windows::ffi::OsStrExt;
442+
use crate::common::windows::ToWideString;
443443
use windows_sys::Win32::Globalization::{
444444
CP_OEMCP, WC_NO_BEST_FIT_CHARS, WideCharToMultiByte,
445445
};
@@ -461,7 +461,7 @@ mod _codecs {
461461
}
462462

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

466466
// Get the required buffer size
467467
let size = unsafe {

crates/vm/src/stdlib/nt.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@ pub(crate) mod module {
1515
use crate::{
1616
PyResult, TryFromObject, VirtualMachine,
1717
builtins::{PyBaseExceptionRef, PyDictRef, PyListRef, PyStrRef, PyTupleRef},
18-
common::{crt_fd, os::last_os_error, suppress_iph},
18+
common::{crt_fd, os::last_os_error, suppress_iph, windows::ToWideString},
1919
convert::ToPyException,
2020
function::{Either, OptionalArg},
2121
ospath::OsPath,
2222
stdlib::os::{_os, DirFd, FollowSymlinks, SupportFunc, TargetIsDirectory, errno_err},
2323
};
2424
use libc::intptr_t;
2525
use std::os::windows::io::AsRawHandle;
26-
use std::{
27-
env, fs, io,
28-
mem::MaybeUninit,
29-
os::windows::ffi::{OsStrExt, OsStringExt},
30-
};
26+
use std::{env, fs, io, mem::MaybeUninit, os::windows::ffi::OsStringExt};
3127
use windows_sys::Win32::{
3228
Foundation::{self, INVALID_HANDLE_VALUE},
3329
Storage::FileSystem,
@@ -541,7 +537,7 @@ pub(crate) mod module {
541537

542538
#[pyfunction]
543539
fn _path_splitroot(path: OsPath, vm: &VirtualMachine) -> PyResult<(String, String)> {
544-
let orig: Vec<_> = path.path.encode_wide().collect();
540+
let orig: Vec<_> = path.path.to_wide();
545541
if orig.is_empty() {
546542
return Ok(("".to_owned(), "".to_owned()));
547543
}

crates/vm/src/stdlib/sys.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod sys {
1212
common::{
1313
ascii,
1414
hash::{PyHash, PyUHash},
15+
windows::ToWideString,
1516
},
1617
convert::ToPyObject,
1718
frame::FrameRef,
@@ -22,8 +23,6 @@ mod sys {
2223
vm::{Settings, VirtualMachine},
2324
};
2425
use num_traits::ToPrimitive;
25-
#[cfg(windows)]
26-
use std::os::windows::ffi::OsStrExt;
2726
use std::{
2827
env::{self, VarError},
2928
io::Read,
@@ -553,10 +552,7 @@ mod sys {
553552
fn get_kernel32_version() -> std::io::Result<(u32, u32, u32)> {
554553
unsafe {
555554
// Create a wide string for "kernel32.dll"
556-
let module_name: Vec<u16> = std::ffi::OsStr::new("kernel32.dll")
557-
.encode_wide()
558-
.chain(Some(0))
559-
.collect();
555+
let module_name: Vec<u16> = std::ffi::OsStr::new("kernel32.dll").to_wide_with_nul();
560556
let h_kernel32 = GetModuleHandleW(module_name.as_ptr());
561557
if h_kernel32.is_null() {
562558
return Err(std::io::Error::last_os_error());
@@ -593,10 +589,7 @@ mod sys {
593589
}
594590

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

601594
let mut ffi_ptr: *mut VS_FIXEDFILEINFO = std::ptr::null_mut();
602595
let mut ffi_len: u32 = 0;

crates/vm/src/windows.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
convert::{ToPyObject, ToPyResult},
88
stdlib::os::errno_err,
99
};
10+
use rustpython_common::windows::ToWideString;
1011
use std::ffi::OsStr;
1112
use windows::Win32::Foundation::HANDLE;
1213
use windows_sys::Win32::Foundation::{BOOL, HANDLE as RAW_HANDLE, INVALID_HANDLE_VALUE};
@@ -234,13 +235,12 @@ fn attributes_from_dir(
234235
windows_sys::Win32::Storage::FileSystem::BY_HANDLE_FILE_INFORMATION,
235236
u32,
236237
)> {
237-
use std::os::windows::ffi::OsStrExt;
238238
use windows_sys::Win32::Storage::FileSystem::{
239239
BY_HANDLE_FILE_INFORMATION, FILE_ATTRIBUTE_REPARSE_POINT, FindClose, FindFirstFileW,
240240
WIN32_FIND_DATAW,
241241
};
242242

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

246246
let handle = unsafe { FindFirstFileW(wide.as_ptr(), &mut find_data) };
@@ -268,7 +268,6 @@ fn attributes_from_dir(
268268

269269
/// Ported from win32_xstat_slow_impl
270270
fn win32_xstat_slow_impl(path: &OsStr, traverse: bool) -> std::io::Result<StatStruct> {
271-
use std::os::windows::ffi::OsStrExt;
272271
use windows_sys::Win32::{
273272
Foundation::{
274273
CloseHandle, ERROR_ACCESS_DENIED, ERROR_CANT_ACCESS_FILE, ERROR_INVALID_FUNCTION,
@@ -286,7 +285,7 @@ fn win32_xstat_slow_impl(path: &OsStr, traverse: bool) -> std::io::Result<StatSt
286285
},
287286
};
288287

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

291290
let access = FILE_READ_ATTRIBUTES;
292291
let mut flags = FILE_FLAG_BACKUP_SEMANTICS;

0 commit comments

Comments
 (0)