Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add os.fchdir builtin function #3017

Merged
merged 3 commits into from
Sep 7, 2021
Merged
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
Prev Previous commit
Move os.fchdir to posix module
  • Loading branch information
ChJR committed Sep 7, 2021
commit c9c7d8f430eb4ecb239e35466a404901b6b9edb7
24 changes: 6 additions & 18 deletions vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 10,13 @@ use std::{env, fs};
use crate::crt_fd::Fd;
use crossbeam_utils::atomic::AtomicCell;
use num_bigint::BigInt;
use num_traits::ToPrimitive;
#[cfg(unix)]
use strum_macros::EnumString;

use super::errno::errors;
use crate::builtins::bytes::{PyBytes, PyBytesRef};
use crate::builtins::dict::PyDictRef;
use crate::builtins::int;
use crate::builtins::int::PyIntRef;
use crate::builtins::pystr::{PyStr, PyStrRef};
use crate::builtins::pytype::PyTypeRef;
use crate::builtins::set::PySet;
Expand Down Expand Up @@ -1270,20 1268,6 @@ mod _os {
env::set_current_dir(&path.path).map_err(|err| err.into_pyexception(vm))
}

#[cfg(not(windows))]
#[pyfunction]
fn fchdir(fd: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
let ret = match fd.as_bigint().to_i32() {
Some(fd) => unsafe { libc::fchdir(fd) },
None => -1,
};
if ret < 0 {
Err(io::Error::last_os_error().into_pyexception(vm))
} else {
Ok(())
}
}

#[pyfunction]
fn fspath(path: PyObjectRef, vm: &VirtualMachine) -> PyResult<FsPath> {
super::fspath(path, false, vm)
Expand Down Expand Up @@ -1955,8 1939,6 @@ mod _os {
SupportFunc::new("open", Some(false), Some(OPEN_DIR_FD), Some(false)),
SupportFunc::new("access", Some(false), Some(false), None),
SupportFunc::new("chdir", None, Some(false), Some(false)),
#[cfg(not(windows))]
SupportFunc::new("fchdir", None, Some(true), Some(false)),
// chflags Some, None Some
SupportFunc::new("listdir", Some(LISTDIR_FD), Some(false), Some(false)),
SupportFunc::new("mkdir", Some(false), Some(MKDIR_DIR_FD), Some(false)),
Expand Down Expand Up @@ -2335,6 2317,12 @@ mod posix {
}
}

#[cfg(not(target_os = "redox"))]
#[pyfunction]
fn fchdir(fd: RawFd, vm: &VirtualMachine) -> PyResult<()> {
nix::unistd::fchdir(fd).map_err(|err| err.into_pyexception(vm))
}

#[cfg(not(target_os = "redox"))]
#[pyfunction]
fn chroot(path: PyPathLike, vm: &VirtualMachine) -> PyResult<()> {
Expand Down