Add os.openpty

This commit is contained in:
yf
2019-08-04 18:26:15 +08:00
parent 6cc4ebb829
commit 18bbe94753
2 changed files with 30 additions and 11 deletions

View File

@@ -274,3 +274,10 @@ if "win" not in sys.platform:
assert_raises(PermissionError, lambda: os.setpgid(os.getpid(), 42))
assert_raises(PermissionError, lambda: os.setuid(42))
assert_raises(PermissionError, lambda: os.seteuid(42))
# pty
a, b = os.openpty()
assert isinstance(a, int)
assert isinstance(b, int)
os.close(b)
os.close(a)

View File

@@ -1,17 +1,20 @@
use std::{env, fs};
use std::cell::RefCell;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::{self, ErrorKind, Read, Write};
use std::time::{Duration, SystemTime};
use std::{env, fs};
use bitflags::bitflags;
#[cfg(unix)]
use nix::errno::Errno;
#[cfg(unix)]
use nix::pty::openpty;
#[cfg(unix)]
use nix::unistd::{self, Gid, Pid, Uid};
use num_traits::cast::ToPrimitive;
use bitflags::bitflags;
use crate::function::{IntoPyNativeFunc, PyFuncArgs};
use crate::obj::objbytes::PyBytesRef;
use crate::obj::objdict::PyDictRef;
@@ -429,8 +432,8 @@ fn os_scandir(path: PyStringRef, vm: &VirtualMachine) -> PyResult {
Ok(iter) => Ok(ScandirIterator {
entries: RefCell::new(iter),
}
.into_ref(vm)
.into_object()),
.into_ref(vm)
.into_object()),
Err(s) => Err(convert_io_error(vm, s)),
}
}
@@ -649,11 +652,11 @@ fn os_stat(
}
#[cfg(not(any(
target_os = "linux",
target_os = "macos",
target_os = "android",
target_os = "redox",
windows
target_os = "linux",
target_os = "macos",
target_os = "android",
target_os = "redox",
windows
)))]
fn os_stat(
_path: PyStringRef,
@@ -849,6 +852,14 @@ fn os_seteuid(euid: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
unistd::seteuid(Uid::from_raw(euid)).map_err(|err| convert_nix_error(vm, err))
}
#[cfg(unix)]
pub fn os_openpty(vm: &VirtualMachine) -> PyResult {
match openpty(None, None) {
Ok(r) => Ok(vm.ctx.new_tuple(vec![vm.new_int(r.master), vm.new_int(r.slave)])),
Err(err) => Err(convert_nix_error(vm, err)),
}
}
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let ctx = &vm.ctx;
@@ -901,8 +912,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
dir_fd: Option<bool>,
follow_symlinks: Option<bool>,
) -> Self
where
F: IntoPyNativeFunc<T, R>,
where
F: IntoPyNativeFunc<T, R>,
{
let func_obj = vm.ctx.new_rustfunc(func);
Self {
@@ -1025,6 +1036,7 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) ->
"setsid" => ctx.new_rustfunc(os_setsid),
"setuid" => ctx.new_rustfunc(os_setuid),
"seteuid" => ctx.new_rustfunc(os_seteuid),
"openpty" => ctx.new_rustfunc(os_openpty),
});
module