Skip to content

Commit 52f8981

Browse files
committed
Windows fixes
1 parent cadba1b commit 52f8981

File tree

6 files changed

+40
-51
lines changed

6 files changed

+40
-51
lines changed

common/src/crt_fd.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,13 @@ pub fn ftruncate(fd: Borrowed<'_>, len: Offset) -> io::Result<()> {
335335

336336
#[cfg(windows)]
337337
pub fn as_handle(fd: Borrowed<'_>) -> io::Result<BorrowedHandle<'_>> {
338+
use windows_sys::Win32::Foundation::{HANDLE, INVALID_HANDLE_VALUE};
338339
unsafe extern "C" {
339340
fn _get_osfhandle(fd: Borrowed<'_>) -> c::intptr_t;
340341
}
341342
let handle = unsafe { suppress_iph!(_get_osfhandle(fd)) };
342-
if handle == -1 {
343-
Err(io::Error::last_os_error())
343+
if handle as HANDLE == INVALID_HANDLE_VALUE {
344+
Err(crate::os::last_os_error())
344345
} else {
345346
Ok(unsafe { BorrowedHandle::borrow_raw(handle as _) })
346347
}

common/src/fileutils.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ pub use libc::stat as StatStruct;
88
pub use windows::{StatStruct, fstat};
99

1010
#[cfg(not(windows))]
11-
pub fn fstat(fd: libc::c_int) -> std::io::Result<StatStruct> {
11+
pub fn fstat(fd: crate::crt_fd::Borrowed<'_>) -> std::io::Result<StatStruct> {
1212
let mut stat = std::mem::MaybeUninit::uninit();
1313
unsafe {
14-
let ret = libc::fstat(fd, stat.as_mut_ptr());
14+
let ret = libc::fstat(fd.as_raw(), stat.as_mut_ptr());
1515
if ret == -1 {
1616
Err(crate::os::last_os_error())
1717
} else {
@@ -22,15 +22,15 @@ pub fn fstat(fd: libc::c_int) -> std::io::Result<StatStruct> {
2222

2323
#[cfg(windows)]
2424
pub mod windows {
25-
use crate::suppress_iph;
25+
use crate::crt_fd;
2626
use crate::windows::ToWideString;
2727
use libc::{S_IFCHR, S_IFDIR, S_IFMT};
2828
use std::ffi::{CString, OsStr, OsString};
2929
use std::os::windows::ffi::OsStrExt;
30+
use std::os::windows::io::AsRawHandle;
3031
use std::sync::OnceLock;
3132
use windows_sys::Win32::Foundation::{
32-
BOOL, ERROR_INVALID_HANDLE, ERROR_NOT_SUPPORTED, FILETIME, FreeLibrary, HANDLE,
33-
INVALID_HANDLE_VALUE, SetLastError,
33+
BOOL, ERROR_INVALID_HANDLE, ERROR_NOT_SUPPORTED, FILETIME, FreeLibrary, SetLastError,
3434
};
3535
use windows_sys::Win32::Storage::FileSystem::{
3636
BY_HANDLE_FILE_INFORMATION, FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_READONLY,
@@ -94,26 +94,14 @@ pub mod windows {
9494
}
9595
}
9696

97-
unsafe extern "C" {
98-
fn _get_osfhandle(fd: i32) -> libc::intptr_t;
99-
}
100-
101-
fn get_osfhandle(fd: i32) -> std::io::Result<isize> {
102-
let ret = unsafe { suppress_iph!(_get_osfhandle(fd)) };
103-
if ret as HANDLE == INVALID_HANDLE_VALUE {
104-
Err(crate::os::last_os_error())
105-
} else {
106-
Ok(ret)
107-
}
108-
}
109-
11097
// _Py_fstat_noraise in cpython
111-
pub fn fstat(fd: libc::c_int) -> std::io::Result<StatStruct> {
112-
let h = get_osfhandle(fd);
98+
pub fn fstat(fd: crt_fd::Borrowed<'_>) -> std::io::Result<StatStruct> {
99+
let h = crt_fd::as_handle(fd);
113100
if h.is_err() {
114101
unsafe { SetLastError(ERROR_INVALID_HANDLE) };
115102
}
116103
let h = h?;
104+
let h = h.as_raw_handle();
117105
// reset stat?
118106

119107
let file_type = unsafe { GetFileType(h as _) };

vm/src/stdlib/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4251,7 +4251,7 @@ mod fileio {
42514251

42524252
// TODO: _Py_set_inheritable
42534253

4254-
let fd_fstat = crate::common::fileutils::fstat(fd.as_raw());
4254+
let fd_fstat = crate::common::fileutils::fstat(fd);
42554255

42564256
#[cfg(windows)]
42574257
{

vm/src/stdlib/msvcrt.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,21 @@ mod msvcrt {
77
use crate::{
88
PyRef, PyResult, VirtualMachine,
99
builtins::{PyBytes, PyStrRef},
10-
common::suppress_iph,
10+
common::{crt_fd, suppress_iph},
11+
convert::IntoPyException,
1112
stdlib::os::errno_err,
1213
};
1314
use itertools::Itertools;
14-
use windows_sys::Win32::{
15-
Foundation::{HANDLE, INVALID_HANDLE_VALUE},
16-
System::Diagnostics::Debug,
17-
};
15+
use std::os::windows::io::AsRawHandle;
16+
use windows_sys::Win32::System::Diagnostics::Debug;
1817

1918
#[pyattr]
2019
use windows_sys::Win32::System::Diagnostics::Debug::{
2120
SEM_FAILCRITICALERRORS, SEM_NOALIGNMENTFAULTEXCEPT, SEM_NOGPFAULTERRORBOX,
2221
SEM_NOOPENFILEERRORBOX,
2322
};
2423

25-
pub fn setmode_binary(fd: i32) {
24+
pub fn setmode_binary(fd: crt_fd::Borrowed<'_>) {
2625
unsafe { suppress_iph!(_setmode(fd, libc::O_BINARY)) };
2726
}
2827

@@ -73,11 +72,11 @@ mod msvcrt {
7372
}
7473

7574
unsafe extern "C" {
76-
fn _setmode(fd: i32, flags: i32) -> i32;
75+
fn _setmode(fd: crt_fd::Borrowed<'_>, flags: i32) -> i32;
7776
}
7877

7978
#[pyfunction]
80-
fn setmode(fd: i32, flags: i32, vm: &VirtualMachine) -> PyResult<i32> {
79+
fn setmode(fd: crt_fd::Borrowed<'_>, flags: i32, vm: &VirtualMachine) -> PyResult<i32> {
8180
let flags = unsafe { suppress_iph!(_setmode(fd, flags)) };
8281
if flags == -1 {
8382
Err(errno_err(vm))
@@ -88,7 +87,6 @@ mod msvcrt {
8887

8988
unsafe extern "C" {
9089
fn _open_osfhandle(osfhandle: isize, flags: i32) -> i32;
91-
fn _get_osfhandle(fd: i32) -> libc::intptr_t;
9290
}
9391

9492
#[pyfunction]
@@ -102,13 +100,10 @@ mod msvcrt {
102100
}
103101

104102
#[pyfunction]
105-
fn get_osfhandle(fd: i32, vm: &VirtualMachine) -> PyResult<isize> {
106-
let ret = unsafe { suppress_iph!(_get_osfhandle(fd)) };
107-
if ret as HANDLE == INVALID_HANDLE_VALUE {
108-
Err(errno_err(vm))
109-
} else {
110-
Ok(ret)
111-
}
103+
fn get_osfhandle(fd: crt_fd::Borrowed<'_>, vm: &VirtualMachine) -> PyResult<isize> {
104+
crt_fd::as_handle(fd)
105+
.map(|h| h.as_raw_handle() as _)
106+
.map_err(|e| e.into_pyexception(vm))
112107
}
113108

114109
#[allow(non_snake_case)]

vm/src/stdlib/nt.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ pub(crate) mod module {
1515
use crate::{
1616
PyResult, TryFromObject, VirtualMachine,
1717
builtins::{PyDictRef, PyListRef, PyStrRef, PyTupleRef},
18-
common::{crt_fd::Fd, os::last_os_error, suppress_iph},
18+
common::{crt_fd, os::last_os_error, suppress_iph},
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;
25+
use std::os::windows::io::AsRawHandle;
2526
use std::{
2627
env, fs, io,
2728
mem::MaybeUninit,
@@ -61,17 +62,17 @@ pub(crate) mod module {
6162
}
6263

6364
#[derive(FromArgs)]
64-
pub(super) struct SymlinkArgs {
65+
pub(super) struct SymlinkArgs<'fd> {
6566
src: OsPath,
6667
dst: OsPath,
6768
#[pyarg(flatten)]
6869
target_is_directory: TargetIsDirectory,
6970
#[pyarg(flatten)]
70-
_dir_fd: DirFd<{ _os::SYMLINK_DIR_FD as usize }>,
71+
_dir_fd: DirFd<'fd, { _os::SYMLINK_DIR_FD as usize }>,
7172
}
7273

7374
#[pyfunction]
74-
pub(super) fn symlink(args: SymlinkArgs, vm: &VirtualMachine) -> PyResult<()> {
75+
pub(super) fn symlink(args: SymlinkArgs<'_>, vm: &VirtualMachine) -> PyResult<()> {
7576
use std::os::windows::fs as win_fs;
7677
let dir = args.target_is_directory.target_is_directory
7778
|| args
@@ -89,9 +90,13 @@ pub(crate) mod module {
8990
}
9091

9192
#[pyfunction]
92-
fn set_inheritable(fd: i32, inheritable: bool, vm: &VirtualMachine) -> PyResult<()> {
93-
let handle = Fd(fd).to_raw_handle().map_err(|e| e.to_pyexception(vm))?;
94-
set_handle_inheritable(handle as _, inheritable, vm)
93+
fn set_inheritable(
94+
fd: crt_fd::Borrowed<'_>,
95+
inheritable: bool,
96+
vm: &VirtualMachine,
97+
) -> PyResult<()> {
98+
let handle = crt_fd::as_handle(fd).map_err(|e| e.to_pyexception(vm))?;
99+
set_handle_inheritable(handle.as_raw_handle() as _, inheritable, vm)
95100
}
96101

97102
#[pyattr]
@@ -107,7 +112,7 @@ pub(crate) mod module {
107112
#[pyfunction]
108113
fn chmod(
109114
path: OsPath,
110-
dir_fd: DirFd<0>,
115+
dir_fd: DirFd<'_, 0>,
111116
mode: u32,
112117
follow_symlinks: FollowSymlinks,
113118
vm: &VirtualMachine,
@@ -458,7 +463,7 @@ pub(crate) mod module {
458463
fn mkdir(
459464
path: OsPath,
460465
mode: OptionalArg<i32>,
461-
dir_fd: DirFd<{ _os::MKDIR_DIR_FD as usize }>,
466+
dir_fd: DirFd<'_, { _os::MKDIR_DIR_FD as usize }>,
462467
vm: &VirtualMachine,
463468
) -> PyResult<()> {
464469
let mode = mode.unwrap_or(0o777);

vm/src/stdlib/os.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub(super) mod _os {
247247
let [] = dir_fd.0;
248248
let name = name.to_wide_cstring(vm)?;
249249
let flags = flags | libc::O_NOINHERIT;
250-
Fd::wopen(&name, flags, mode)
250+
crt_fd::wopen(&name, flags, mode)
251251
};
252252
#[cfg(not(windows))]
253253
let fd = {
@@ -841,8 +841,8 @@ pub(super) mod _os {
841841

842842
#[cfg(windows)]
843843
fn stat_inner(
844-
file: OsPathOrFd,
845-
dir_fd: DirFd<{ STAT_DIR_FD as usize }>,
844+
file: OsPathOrFd<'_>,
845+
dir_fd: DirFd<'_, { STAT_DIR_FD as usize }>,
846846
follow_symlinks: FollowSymlinks,
847847
) -> io::Result<Option<StatStruct>> {
848848
// TODO: replicate CPython's win32_xstat

0 commit comments

Comments
 (0)