Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 0 additions & 3 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,6 @@ def test_communicate_returns(self):
self.assertEqual(stdout, None)
self.assertEqual(stderr, None)

@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
def test_communicate_pipe_buf(self):
# communicate() with writes larger than pipe_buf
# This test will probably deadlock rather than fail, if
Expand Down Expand Up @@ -3564,8 +3563,6 @@ def test_close_fds(self):
close_fds=True)
self.assertEqual(rc, 47)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_close_fds_with_stdio(self):
import msvcrt

Expand Down
10 changes: 2 additions & 8 deletions crates/common/src/fileutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,22 +460,16 @@ pub fn fopen(path: &std::path::Path, mode: &str) -> std::io::Result<*mut libc::F
{
use std::os::windows::io::IntoRawHandle;

// Declare Windows CRT functions
unsafe extern "C" {
fn _open_osfhandle(handle: isize, flags: libc::c_int) -> libc::c_int;
fn _fdopen(fd: libc::c_int, mode: *const libc::c_char) -> *mut libc::FILE;
}

// Convert File handle to CRT file descriptor
let handle = file.into_raw_handle();
let fd = unsafe { _open_osfhandle(handle as isize, libc::O_RDONLY) };
let fd = unsafe { libc::open_osfhandle(handle as isize, libc::O_RDONLY) };
if fd == -1 {
return Err(std::io::Error::last_os_error());
}

// Convert fd to FILE*
let mode_cstr = CString::new(mode).unwrap();
let fp = unsafe { _fdopen(fd, mode_cstr.as_ptr()) };
let fp = unsafe { libc::fdopen(fd, mode_cstr.as_ptr()) };
if fp.is_null() {
unsafe { libc::close(fd) };
return Err(std::io::Error::last_os_error());
Expand Down
6 changes: 1 addition & 5 deletions crates/vm/src/stdlib/msvcrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,9 @@ mod msvcrt {
}
}

unsafe extern "C" {
fn _open_osfhandle(osfhandle: isize, flags: i32) -> i32;
}

#[pyfunction]
fn open_osfhandle(handle: isize, flags: i32, vm: &VirtualMachine) -> PyResult<i32> {
let ret = unsafe { suppress_iph!(_open_osfhandle(handle, flags)) };
let ret = unsafe { suppress_iph!(libc::open_osfhandle(handle, flags)) };
if ret == -1 {
Err(errno_err(vm))
} else {
Expand Down
113 changes: 109 additions & 4 deletions crates/vm/src/stdlib/nt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,8 +833,6 @@ pub(crate) mod module {

unsafe extern "C" {
fn _umask(mask: i32) -> i32;
fn _dup(fd: i32) -> i32;
fn _dup2(fd: i32, fd2: i32) -> i32;
}

/// Close fd and convert error to PyException (PEP 446 cleanup)
Expand All @@ -854,9 +852,116 @@ pub(crate) mod module {
}
}

#[pyfunction]
fn pipe(vm: &VirtualMachine) -> PyResult<(i32, i32)> {
use windows_sys::Win32::System::Pipes::CreatePipe;

let (read_handle, write_handle) = unsafe {
let mut read = MaybeUninit::<isize>::uninit();
let mut write = MaybeUninit::<isize>::uninit();
let res = CreatePipe(
read.as_mut_ptr() as *mut _,
write.as_mut_ptr() as *mut _,
std::ptr::null(),
0,
);
if res == 0 {
return Err(errno_err(vm));
}
(read.assume_init(), write.assume_init())
};

// Convert handles to file descriptors
// O_NOINHERIT = 0x80 (MSVC CRT)
const O_NOINHERIT: i32 = 0x80;
let read_fd = unsafe { libc::open_osfhandle(read_handle, O_NOINHERIT) };
let write_fd = unsafe { libc::open_osfhandle(write_handle, libc::O_WRONLY | O_NOINHERIT) };

if read_fd == -1 || write_fd == -1 {
unsafe {
Foundation::CloseHandle(read_handle as _);
Foundation::CloseHandle(write_handle as _);
}
return Err(errno_err(vm));
}

Ok((read_fd, write_fd))
}

#[pyfunction]
fn getppid() -> u32 {
use windows_sys::Win32::System::Threading::GetCurrentProcess;

#[repr(C)]
struct ProcessBasicInformation {
exit_status: isize,
peb_base_address: *mut std::ffi::c_void,
affinity_mask: usize,
base_priority: i32,
unique_process_id: usize,
inherited_from_unique_process_id: usize,
}

type NtQueryInformationProcessFn = unsafe extern "system" fn(
process_handle: isize,
process_information_class: u32,
process_information: *mut std::ffi::c_void,
process_information_length: u32,
return_length: *mut u32,
) -> i32;

let ntdll = unsafe {
windows_sys::Win32::System::LibraryLoader::GetModuleHandleW(windows_sys::w!(
"ntdll.dll"
))
};
if ntdll.is_null() {
return 0;
}

let func = unsafe {
windows_sys::Win32::System::LibraryLoader::GetProcAddress(
ntdll,
c"NtQueryInformationProcess".as_ptr() as *const u8,
)
};
let Some(func) = func else {
return 0;
};
let nt_query: NtQueryInformationProcessFn = unsafe { std::mem::transmute(func) };

let mut info = ProcessBasicInformation {
exit_status: 0,
peb_base_address: std::ptr::null_mut(),
affinity_mask: 0,
base_priority: 0,
unique_process_id: 0,
inherited_from_unique_process_id: 0,
};

let status = unsafe {
nt_query(
GetCurrentProcess() as isize,
0, // ProcessBasicInformation
&mut info as *mut _ as *mut std::ffi::c_void,
std::mem::size_of::<ProcessBasicInformation>() as u32,
std::ptr::null_mut(),
)
};

if status >= 0
&& info.inherited_from_unique_process_id != 0
&& info.inherited_from_unique_process_id < u32::MAX as usize
{
info.inherited_from_unique_process_id as u32
} else {
0
}
}

#[pyfunction]
fn dup(fd: i32, vm: &VirtualMachine) -> PyResult<i32> {
let fd2 = unsafe { suppress_iph!(_dup(fd)) };
let fd2 = unsafe { suppress_iph!(libc::dup(fd)) };
if fd2 < 0 {
return Err(errno_err(vm));
}
Expand All @@ -879,7 +984,7 @@ pub(crate) mod module {

#[pyfunction]
fn dup2(args: Dup2Args, vm: &VirtualMachine) -> PyResult<i32> {
let result = unsafe { suppress_iph!(_dup2(args.fd, args.fd2)) };
let result = unsafe { suppress_iph!(libc::dup2(args.fd, args.fd2)) };
if result < 0 {
return Err(errno_err(vm));
}
Expand Down
Loading