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
get_inheritable, dup for windows
  • Loading branch information
youknowone committed Dec 8, 2025
commit 111504628f4a5149746459627975ea5265215eb3
1 change: 0 additions & 1 deletion Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,6 @@ def test_open_default_encoding(self):
os.environ.clear()
os.environ.update(old_environ)

@unittest.expectedFailureIfWindows('TODO: RUSTPYTHON Windows')
@support.requires_subprocess()
def test_open_non_inheritable(self):
fileobj = open(__file__, encoding="utf-8")
Expand Down
5 changes: 0 additions & 5 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -4621,7 +4621,6 @@ def test_process_cpu_count_affinity(self):
# FD inheritance check is only useful for systems with process support.
@support.requires_subprocess()
class FDInheritanceTests(unittest.TestCase):
@unittest.expectedFailureIfWindows('TODO: RUSTPYTHON; os.get_inheritable not implemented yet for all platforms')
def test_get_set_inheritable(self):
fd = os.open(__file__, os.O_RDONLY)
self.addCleanup(os.close, fd)
Expand Down Expand Up @@ -4682,7 +4681,6 @@ def test_get_set_inheritable_badf(self):
os.set_inheritable(fd, False)
self.assertEqual(ctx.exception.errno, errno.EBADF)

@unittest.expectedFailureIfWindows('TODO: RUSTPYTHON; os.get_inheritable not implemented yet for all platforms')
def test_open(self):
fd = os.open(__file__, os.O_RDONLY)
self.addCleanup(os.close, fd)
Expand All @@ -4696,7 +4694,6 @@ def test_pipe(self):
self.assertEqual(os.get_inheritable(rfd), False)
self.assertEqual(os.get_inheritable(wfd), False)

@unittest.skipIf(sys.platform == 'win32', 'TODO: RUSTPYTHON; os.dup on windows')
def test_dup(self):
fd1 = os.open(__file__, os.O_RDONLY)
self.addCleanup(os.close, fd1)
Expand All @@ -4705,13 +4702,11 @@ def test_dup(self):
self.addCleanup(os.close, fd2)
self.assertEqual(os.get_inheritable(fd2), False)

@unittest.skipIf(sys.platform == 'win32', 'TODO: RUSTPYTHON; os.dup on windows')
def test_dup_standard_stream(self):
fd = os.dup(1)
self.addCleanup(os.close, fd)
self.assertGreater(fd, 0)

@unittest.expectedFailureIfWindows('TODO: RUSTPYTHON; os.dup not implemented yet for all platforms')
@unittest.skipUnless(sys.platform == 'win32', 'win32-specific test')
def test_dup_nul(self):
# os.dup() was creating inheritable fds for character files.
Expand Down
48 changes: 48 additions & 0 deletions crates/vm/src/stdlib/nt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,13 @@ pub(crate) mod module {
}
}

#[pyfunction]
fn get_inheritable(fd: i32, vm: &VirtualMachine) -> PyResult<bool> {
let borrowed = unsafe { crt_fd::Borrowed::borrow_raw(fd) };
let handle = crt_fd::as_handle(borrowed).map_err(|e| e.to_pyexception(vm))?;
get_handle_inheritable(handle.as_raw_handle() as _, vm)
}

#[pyfunction]
fn getlogin(vm: &VirtualMachine) -> PyResult<String> {
let mut buffer = [0u16; 257];
Expand Down Expand Up @@ -477,6 +484,8 @@ pub(crate) mod module {

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

#[pyfunction]
Expand All @@ -489,6 +498,45 @@ pub(crate) mod module {
}
}

#[pyfunction]
fn dup(fd: i32, vm: &VirtualMachine) -> PyResult<i32> {
let fd2 = unsafe { suppress_iph!(_dup(fd)) };
if fd2 < 0 {
return Err(errno_err(vm));
}
// Set the new fd as non-inheritable
let borrowed = unsafe { crt_fd::Borrowed::borrow_raw(fd2) };
let handle = crt_fd::as_handle(borrowed).map_err(|e| e.to_pyexception(vm))?;
raw_set_handle_inheritable(handle.as_raw_handle() as _, false)
.map_err(|e| e.to_pyexception(vm))?;
Ok(fd2)
}

#[derive(FromArgs)]
struct Dup2Args {
#[pyarg(positional)]
fd: i32,
#[pyarg(positional)]
fd2: i32,
#[pyarg(any, default = true)]
inheritable: bool,
}

#[pyfunction]
fn dup2(args: Dup2Args, vm: &VirtualMachine) -> PyResult<i32> {
let result = unsafe { suppress_iph!(_dup2(args.fd, args.fd2)) };
if result < 0 {
return Err(errno_err(vm));
}
if !args.inheritable {
let borrowed = unsafe { crt_fd::Borrowed::borrow_raw(args.fd2) };
let handle = crt_fd::as_handle(borrowed).map_err(|e| e.to_pyexception(vm))?;
raw_set_handle_inheritable(handle.as_raw_handle() as _, false)
.map_err(|e| e.to_pyexception(vm))?;
}
Ok(args.fd2)
}

pub(crate) fn support_funcs() -> Vec<SupportFunc> {
Vec::new()
}
Expand Down