rename PyPathLike -> OsPath becasue it is not a python object

This commit is contained in:
Jeong YunWon
2023-03-28 16:29:07 +09:00
parent 3d1a74dda9
commit 7b08953c18
6 changed files with 97 additions and 96 deletions

View File

@@ -1,7 +1,7 @@
use crate::vm::{
builtins::PyListRef,
function::ArgSequence,
stdlib::{os::PyPathLike, posix},
stdlib::{os::OsPath, posix},
{PyObjectRef, PyResult, TryFromObject, VirtualMachine},
};
use nix::{errno::Errno, unistd};
@@ -60,7 +60,7 @@ struct CStrPathLike {
}
impl TryFromObject for CStrPathLike {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let s = PyPathLike::try_from_object(vm, obj)?.into_cstring(vm)?;
let s = OsPath::try_from_object(vm, obj)?.into_cstring(vm)?;
Ok(CStrPathLike { s })
}
}

View File

@@ -3541,7 +3541,7 @@ mod _io {
// check file descriptor validity
#[cfg(unix)]
if let Ok(crate::stdlib::os::PathOrFd::Fd(fd)) = file.clone().try_into_value(vm) {
if let Ok(crate::stdlib::os::OsPathOrFd::Fd(fd)) = file.clone().try_into_value(vm) {
nix::fcntl::fcntl(fd, nix::fcntl::F_GETFD)
.map_err(|_| crate::stdlib::os::errno_err(vm))?;
}

View File

@@ -17,7 +17,7 @@ pub(crate) mod module {
function::Either,
function::OptionalArg,
stdlib::os::{
errno_err, DirFd, FollowSymlinks, PyPathLike, SupportFunc, TargetIsDirectory, _os,
errno_err, DirFd, FollowSymlinks, OsPath, SupportFunc, TargetIsDirectory, _os,
},
PyResult, TryFromObject, VirtualMachine,
};
@@ -35,7 +35,7 @@ pub(crate) mod module {
use libc::{O_BINARY, O_TEMPORARY};
#[pyfunction]
pub(super) fn access(path: PyPathLike, mode: u8, vm: &VirtualMachine) -> PyResult<bool> {
pub(super) fn access(path: OsPath, mode: u8, vm: &VirtualMachine) -> PyResult<bool> {
use um::{fileapi, winnt};
let attr = unsafe { fileapi::GetFileAttributesW(path.to_widecstring(vm)?.as_ptr()) };
Ok(attr != fileapi::INVALID_FILE_ATTRIBUTES
@@ -46,8 +46,8 @@ pub(crate) mod module {
#[derive(FromArgs)]
pub(super) struct SymlinkArgs {
src: PyPathLike,
dst: PyPathLike,
src: OsPath,
dst: OsPath,
#[pyarg(flatten)]
target_is_directory: TargetIsDirectory,
#[pyarg(flatten)]
@@ -90,7 +90,7 @@ pub(crate) mod module {
#[pyfunction]
fn chmod(
path: PyPathLike,
path: OsPath,
dir_fd: DirFd<0>,
mode: u32,
follow_symlinks: FollowSymlinks,
@@ -239,7 +239,7 @@ pub(crate) mod module {
}
#[pyfunction]
fn _getfinalpathname(path: PyPathLike, vm: &VirtualMachine) -> PyResult {
fn _getfinalpathname(path: OsPath, vm: &VirtualMachine) -> PyResult {
let real = path
.as_ref()
.canonicalize()
@@ -248,7 +248,7 @@ pub(crate) mod module {
}
#[pyfunction]
fn _getfullpathname(path: PyPathLike, vm: &VirtualMachine) -> PyResult {
fn _getfullpathname(path: OsPath, vm: &VirtualMachine) -> PyResult {
let wpath = path.to_widecstring(vm)?;
let mut buffer = vec![0u16; winapi::shared::minwindef::MAX_PATH];
let ret = unsafe {
@@ -281,7 +281,7 @@ pub(crate) mod module {
}
#[pyfunction]
fn _getvolumepathname(path: PyPathLike, vm: &VirtualMachine) -> PyResult {
fn _getvolumepathname(path: OsPath, vm: &VirtualMachine) -> PyResult {
let wide = path.to_widecstring(vm)?;
let buflen = std::cmp::max(wide.len(), winapi::shared::minwindef::MAX_PATH);
let mut buffer = vec![0u16; buflen];
@@ -296,7 +296,7 @@ pub(crate) mod module {
}
#[pyfunction]
fn _path_splitroot(path: PyPathLike, vm: &VirtualMachine) -> PyResult<(String, String)> {
fn _path_splitroot(path: OsPath, vm: &VirtualMachine) -> PyResult<(String, String)> {
let orig: Vec<_> = path.path.into_os_string().encode_wide().collect();
if orig.is_empty() {
return Ok(("".to_owned(), "".to_owned()));
@@ -334,7 +334,7 @@ pub(crate) mod module {
}
#[pyfunction]
fn _getdiskusage(path: PyPathLike, vm: &VirtualMachine) -> PyResult<(u64, u64)> {
fn _getdiskusage(path: OsPath, vm: &VirtualMachine) -> PyResult<(u64, u64)> {
use um::fileapi::GetDiskFreeSpaceExW;
use winapi::shared::{ntdef::ULARGE_INTEGER, winerror};
@@ -399,7 +399,7 @@ pub(crate) mod module {
#[pyfunction]
fn mkdir(
path: PyPathLike,
path: OsPath,
mode: OptionalArg<i32>,
dir_fd: DirFd<{ _os::MKDIR_DIR_FD as usize }>,
vm: &VirtualMachine,

View File

@@ -45,13 +45,14 @@ impl OutputMode {
}
}
// path_ without allow_fd in CPython
#[derive(Clone)]
pub struct PyPathLike {
pub struct OsPath {
pub path: PathBuf,
pub(super) mode: OutputMode,
}
impl PyPathLike {
impl OsPath {
pub fn new_str(path: impl Into<PathBuf>) -> Self {
Self {
path: path.into(),
@@ -70,7 +71,6 @@ impl PyPathLike {
self.path.to_string_lossy().to_string().into_bytes()
}
// #[cfg(any(unix, target_os = "wasi"))]
pub fn into_cstring(self, vm: &VirtualMachine) -> PyResult<ffi::CString> {
ffi::CString::new(self.into_bytes()).map_err(|err| err.into_pyexception(vm))
}
@@ -96,24 +96,24 @@ pub(super) fn fs_metadata<P: AsRef<Path>>(
}
}
impl AsRef<Path> for PyPathLike {
impl AsRef<Path> for OsPath {
fn as_ref(&self) -> &Path {
&self.path
}
}
impl FsPath {
pub(crate) fn to_pathlike(&self, vm: &VirtualMachine) -> PyResult<PyPathLike> {
pub(crate) fn to_pathlike(&self, vm: &VirtualMachine) -> PyResult<OsPath> {
let path = self.as_os_str(vm)?.to_owned().into();
let mode = match self {
Self::Str(_) => OutputMode::String,
Self::Bytes(_) => OutputMode::Bytes,
};
Ok(PyPathLike { path, mode })
Ok(OsPath { path, mode })
}
}
impl TryFromObject for PyPathLike {
impl TryFromObject for OsPath {
// TODO: path_converter in CPython
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let fs_path = FsPath::try_from(obj, true, vm)?;
@@ -121,13 +121,14 @@ impl TryFromObject for PyPathLike {
}
}
// path_t with allow_fd in CPython
#[derive(Clone)]
pub(crate) enum PathOrFd {
Path(PyPathLike),
pub(crate) enum OsPathOrFd {
Path(OsPath),
Fd(i32),
}
impl TryFromObject for PathOrFd {
impl TryFromObject for OsPathOrFd {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let r = match obj.downcast::<PyInt>() {
Ok(int) => Self::Fd(int.try_to_primitive(vm)?),
@@ -137,17 +138,17 @@ impl TryFromObject for PathOrFd {
}
}
impl From<PyPathLike> for PathOrFd {
fn from(path: PyPathLike) -> Self {
impl From<OsPath> for OsPathOrFd {
fn from(path: OsPath) -> Self {
Self::Path(path)
}
}
impl PathOrFd {
impl OsPathOrFd {
pub fn filename(&self, vm: &VirtualMachine) -> PyObjectRef {
match self {
PathOrFd::Path(path) => path.filename(vm).unwrap_or_else(|_| vm.ctx.none()),
PathOrFd::Fd(fd) => vm.ctx.new_int(*fd).into(),
OsPathOrFd::Path(path) => path.filename(vm).unwrap_or_else(|_| vm.ctx.none()),
OsPathOrFd::Fd(fd) => vm.ctx.new_int(*fd).into(),
}
}
}
@@ -162,8 +163,8 @@ impl IntoPyException for nix::Error {
// TODO: preserve the input `PyObjectRef` of filename and filename2 (Failing check `self.assertIs(err.filename, name, str(func)`)
pub struct IOErrorBuilder {
error: io::Error,
filename: Option<PathOrFd>,
filename2: Option<PathOrFd>,
filename: Option<OsPathOrFd>,
filename2: Option<OsPathOrFd>,
}
impl IOErrorBuilder {
@@ -174,11 +175,11 @@ impl IOErrorBuilder {
filename2: None,
}
}
pub(crate) fn filename(mut self, filename: impl Into<PathOrFd>) -> Self {
pub(crate) fn filename(mut self, filename: impl Into<OsPathOrFd>) -> Self {
self.filename.replace(filename.into());
self
}
pub(crate) fn filename2(mut self, filename: impl Into<PathOrFd>) -> Self {
pub(crate) fn filename2(mut self, filename: impl Into<OsPathOrFd>) -> Self {
self.filename2.replace(filename.into());
self
}
@@ -296,7 +297,7 @@ fn bytes_as_osstr<'a>(b: &'a [u8], vm: &VirtualMachine) -> PyResult<&'a ffi::OsS
#[pymodule(name = "_os")]
pub(super) mod _os {
use super::{
errno_err, DirFd, FollowSymlinks, IOErrorBuilder, OutputMode, PathOrFd, PyPathLike,
errno_err, DirFd, FollowSymlinks, IOErrorBuilder, OsPath, OsPathOrFd, OutputMode,
SupportFunc,
};
use crate::common::lock::{OnceCell, PyRwLock};
@@ -362,7 +363,7 @@ pub(super) mod _os {
#[cfg(any(unix, windows, target_os = "wasi"))]
#[derive(FromArgs)]
struct OpenArgs {
path: PyPathLike,
path: OsPath,
flags: i32,
#[pyarg(any, default)]
mode: Option<i32>,
@@ -377,7 +378,7 @@ pub(super) mod _os {
#[cfg(any(unix, windows, target_os = "wasi"))]
pub(crate) fn os_open(
name: PyPathLike,
name: OsPath,
flags: i32,
mode: Option<i32>,
dir_fd: DirFd<{ OPEN_DIR_FD as usize }>,
@@ -441,7 +442,7 @@ pub(super) mod _os {
#[pyfunction]
#[pyfunction(name = "unlink")]
fn remove(path: PyPathLike, dir_fd: DirFd<0>, vm: &VirtualMachine) -> PyResult<()> {
fn remove(path: OsPath, dir_fd: DirFd<0>, vm: &VirtualMachine) -> PyResult<()> {
let [] = dir_fd.0;
let is_junction = cfg!(windows)
&& fs::metadata(&path).map_or(false, |meta| meta.file_type().is_dir())
@@ -457,7 +458,7 @@ pub(super) mod _os {
#[cfg(not(windows))]
#[pyfunction]
fn mkdir(
path: PyPathLike,
path: OsPath,
mode: OptionalArg<i32>,
dir_fd: DirFd<{ MKDIR_DIR_FD as usize }>,
vm: &VirtualMachine,
@@ -486,7 +487,7 @@ pub(super) mod _os {
}
#[pyfunction]
fn rmdir(path: PyPathLike, dir_fd: DirFd<0>, vm: &VirtualMachine) -> PyResult<()> {
fn rmdir(path: OsPath, dir_fd: DirFd<0>, vm: &VirtualMachine) -> PyResult<()> {
let [] = dir_fd.0;
fs::remove_dir(&path)
.map_err(|e| IOErrorBuilder::new(e).filename(path).into_pyexception(vm))
@@ -495,10 +496,10 @@ pub(super) mod _os {
const LISTDIR_FD: bool = cfg!(all(unix, not(target_os = "redox")));
#[pyfunction]
fn listdir(path: OptionalArg<PathOrFd>, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
let path = path.unwrap_or_else(|| PathOrFd::Path(PyPathLike::new_str(".")));
fn listdir(path: OptionalArg<OsPathOrFd>, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
let path = path.unwrap_or_else(|| OsPathOrFd::Path(OsPath::new_str(".")));
let list = match path {
PathOrFd::Path(path) => {
OsPathOrFd::Path(path) => {
let dir_iter = fs::read_dir(&path).map_err(|err| err.into_pyexception(vm))?;
dir_iter
.map(|entry| match entry {
@@ -509,7 +510,7 @@ pub(super) mod _os {
})
.collect::<PyResult<_>>()?
}
PathOrFd::Fd(fno) => {
OsPathOrFd::Fd(fno) => {
#[cfg(not(all(unix, not(target_os = "redox"))))]
{
let _ = fno;
@@ -592,7 +593,7 @@ pub(super) mod _os {
}
#[pyfunction]
fn readlink(path: PyPathLike, dir_fd: DirFd<0>, vm: &VirtualMachine) -> PyResult {
fn readlink(path: OsPath, dir_fd: DirFd<0>, vm: &VirtualMachine) -> PyResult {
let mode = path.mode;
let [] = dir_fd.0;
let path = fs::read_link(&path)
@@ -683,7 +684,7 @@ pub(super) mod _os {
) -> PyResult {
let do_stat = |follow_symlinks| {
stat(
PathOrFd::Path(PyPathLike {
OsPathOrFd::Path(OsPath {
path: self.pathval.clone(),
mode: OutputMode::String,
}),
@@ -715,7 +716,7 @@ pub(super) mod _os {
Some(ino) => Ok(ino),
None => {
let stat = stat_inner(
PathOrFd::Path(PyPathLike {
OsPathOrFd::Path(OsPath {
path: self.pathval.clone(),
mode: OutputMode::String,
}),
@@ -845,8 +846,8 @@ pub(super) mod _os {
}
#[pyfunction]
fn scandir(path: OptionalArg<PyPathLike>, vm: &VirtualMachine) -> PyResult {
let path = path.unwrap_or_else(|| PyPathLike::new_str("."));
fn scandir(path: OptionalArg<OsPath>, vm: &VirtualMachine) -> PyResult {
let path = path.unwrap_or_else(|| OsPath::new_str("."));
let entries = fs::read_dir(path.path).map_err(|err| err.into_pyexception(vm))?;
Ok(ScandirIterator {
entries: PyRwLock::new(Some(entries)),
@@ -1027,15 +1028,15 @@ pub(super) mod _os {
#[cfg(windows)]
fn stat_inner(
file: PathOrFd,
file: OsPathOrFd,
dir_fd: DirFd<{ STAT_DIR_FD as usize }>,
follow_symlinks: FollowSymlinks,
) -> io::Result<Option<StatStruct>> {
// TODO: replicate CPython's win32_xstat
let [] = dir_fd.0;
let meta = match file {
PathOrFd::Path(path) => super::fs_metadata(path, follow_symlinks.0)?,
PathOrFd::Fd(fno) => {
OsPathOrFd::Path(path) => super::fs_metadata(path, follow_symlinks.0)?,
OsPathOrFd::Fd(fno) => {
use std::os::windows::io::FromRawHandle;
let handle = Fd(fno).to_raw_handle()?;
let file =
@@ -1048,13 +1049,13 @@ pub(super) mod _os {
#[cfg(not(windows))]
fn stat_inner(
file: PathOrFd,
file: OsPathOrFd,
dir_fd: DirFd<{ STAT_DIR_FD as usize }>,
follow_symlinks: FollowSymlinks,
) -> io::Result<Option<StatStruct>> {
let mut stat = std::mem::MaybeUninit::uninit();
let ret = match file {
PathOrFd::Path(path) => {
OsPathOrFd::Path(path) => {
use rustpython_common::os::ffi::OsStrExt;
let path = path.as_ref().as_os_str().as_bytes();
let path = match ffi::CString::new(path) {
@@ -1082,7 +1083,7 @@ pub(super) mod _os {
}
})
}
PathOrFd::Fd(fd) => unsafe { libc::fstat(fd, stat.as_mut_ptr()) },
OsPathOrFd::Fd(fd) => unsafe { libc::fstat(fd, stat.as_mut_ptr()) },
};
if ret < 0 {
return Err(io::Error::last_os_error());
@@ -1093,7 +1094,7 @@ pub(super) mod _os {
#[pyfunction]
#[pyfunction(name = "fstat")]
fn stat(
file: PathOrFd,
file: OsPathOrFd,
dir_fd: DirFd<{ STAT_DIR_FD as usize }>,
follow_symlinks: FollowSymlinks,
vm: &VirtualMachine,
@@ -1106,7 +1107,7 @@ pub(super) mod _os {
#[pyfunction]
fn lstat(
file: PathOrFd,
file: OsPathOrFd,
dir_fd: DirFd<{ STAT_DIR_FD as usize }>,
vm: &VirtualMachine,
) -> PyResult {
@@ -1128,7 +1129,7 @@ pub(super) mod _os {
}
#[pyfunction]
fn chdir(path: PyPathLike, vm: &VirtualMachine) -> PyResult<()> {
fn chdir(path: OsPath, vm: &VirtualMachine) -> PyResult<()> {
env::set_current_dir(&path.path)
.map_err(|err| IOErrorBuilder::new(err).filename(path).into_pyexception(vm))
}
@@ -1140,7 +1141,7 @@ pub(super) mod _os {
#[pyfunction]
#[pyfunction(name = "replace")]
fn rename(src: PyPathLike, dst: PyPathLike, vm: &VirtualMachine) -> PyResult<()> {
fn rename(src: OsPath, dst: OsPath, vm: &VirtualMachine) -> PyResult<()> {
fs::rename(&src.path, &dst.path).map_err(|err| {
IOErrorBuilder::new(err)
.filename(src)
@@ -1223,7 +1224,7 @@ pub(super) mod _os {
}
#[pyfunction]
fn link(src: PyPathLike, dst: PyPathLike, vm: &VirtualMachine) -> PyResult<()> {
fn link(src: OsPath, dst: OsPath, vm: &VirtualMachine) -> PyResult<()> {
fs::hard_link(&src.path, &dst.path).map_err(|err| {
IOErrorBuilder::new(err)
.filename(src)
@@ -1234,7 +1235,7 @@ pub(super) mod _os {
#[derive(FromArgs)]
struct UtimeArgs {
path: PyPathLike,
path: OsPath,
#[pyarg(any, default)]
times: Option<PyTupleRef>,
#[pyarg(named, default)]
@@ -1303,7 +1304,7 @@ pub(super) mod _os {
}
fn utime_impl(
path: PyPathLike,
path: OsPath,
acc: Duration,
modif: Duration,
dir_fd: DirFd<{ UTIME_DIR_FD as usize }>,
@@ -1531,7 +1532,7 @@ pub(super) mod _os {
if let Ok(fd) = path.try_to_value(vm) {
return ftruncate(fd, length, vm);
}
let path = PyPathLike::try_from_object(vm, path)?;
let path = OsPath::try_from_object(vm, path)?;
// TODO: just call libc::truncate() on POSIX
let f = OpenOptions::new()
.write(true)

View File

@@ -25,7 +25,7 @@ pub mod module {
convert::{IntoPyException, ToPyObject, TryFromObject},
function::{Either, OptionalArg},
stdlib::os::{
errno_err, DirFd, FollowSymlinks, PathOrFd, PyPathLike, SupportFunc, TargetIsDirectory,
errno_err, DirFd, FollowSymlinks, OsPath, OsPathOrFd, SupportFunc, TargetIsDirectory,
_os, fs_metadata, IOErrorBuilder,
},
types::Constructor,
@@ -250,7 +250,7 @@ pub mod module {
}
#[pyfunction]
pub(super) fn access(path: PyPathLike, mode: u8, vm: &VirtualMachine) -> PyResult<bool> {
pub(super) fn access(path: OsPath, mode: u8, vm: &VirtualMachine) -> PyResult<bool> {
use std::os::unix::fs::MetadataExt;
let flags = AccessFlags::from_bits(mode).ok_or_else(|| {
@@ -299,8 +299,8 @@ pub mod module {
#[derive(FromArgs)]
pub(super) struct SymlinkArgs {
src: PyPathLike,
dst: PyPathLike,
src: OsPath,
dst: OsPath,
#[pyarg(flatten)]
_target_is_directory: TargetIsDirectory,
#[pyarg(flatten)]
@@ -336,7 +336,7 @@ pub mod module {
#[cfg(not(target_os = "redox"))]
#[pyfunction]
fn chroot(path: PyPathLike, vm: &VirtualMachine) -> PyResult<()> {
fn chroot(path: OsPath, vm: &VirtualMachine) -> PyResult<()> {
use crate::stdlib::os::IOErrorBuilder;
nix::unistd::chroot(&*path.path).map_err(|err| {
@@ -351,7 +351,7 @@ pub mod module {
#[cfg(not(target_os = "redox"))]
#[pyfunction]
fn chown(
path: PathOrFd,
path: OsPathOrFd,
uid: isize,
gid: isize,
dir_fd: DirFd<1>,
@@ -382,10 +382,10 @@ pub mod module {
let dir_fd = dir_fd.get_opt();
match path {
PathOrFd::Path(ref p) => {
OsPathOrFd::Path(ref p) => {
nix::unistd::fchownat(dir_fd, p.path.as_os_str(), uid, gid, flag)
}
PathOrFd::Fd(fd) => nix::unistd::fchown(fd, uid, gid),
OsPathOrFd::Fd(fd) => nix::unistd::fchown(fd, uid, gid),
}
.map_err(|err| {
// Use `From<nix::Error> for io::Error` when it is available
@@ -397,9 +397,9 @@ pub mod module {
#[cfg(not(target_os = "redox"))]
#[pyfunction]
fn lchown(path: PyPathLike, uid: isize, gid: isize, vm: &VirtualMachine) -> PyResult<()> {
fn lchown(path: OsPath, uid: isize, gid: isize, vm: &VirtualMachine) -> PyResult<()> {
chown(
PathOrFd::Path(path),
OsPathOrFd::Path(path),
uid,
gid,
DirFd::default(),
@@ -412,7 +412,7 @@ pub mod module {
#[pyfunction]
fn fchown(fd: i32, uid: isize, gid: isize, vm: &VirtualMachine) -> PyResult<()> {
chown(
PathOrFd::Fd(fd),
OsPathOrFd::Fd(fd),
uid,
gid,
DirFd::default(),
@@ -428,7 +428,7 @@ pub mod module {
#[derive(FromArgs)]
struct MknodArgs {
#[pyarg(any)]
path: PyPathLike,
path: OsPath,
#[pyarg(any)]
mode: libc::mode_t,
#[pyarg(any)]
@@ -770,7 +770,7 @@ pub mod module {
}
fn _chmod(
path: PyPathLike,
path: OsPath,
dir_fd: DirFd<0>,
mode: u32,
follow_symlinks: FollowSymlinks,
@@ -804,22 +804,22 @@ pub mod module {
#[cfg(not(target_os = "redox"))]
#[pyfunction]
fn chmod(
path: PathOrFd,
path: OsPathOrFd,
dir_fd: DirFd<0>,
mode: u32,
follow_symlinks: FollowSymlinks,
vm: &VirtualMachine,
) -> PyResult<()> {
match path {
PathOrFd::Path(path) => _chmod(path, dir_fd, mode, follow_symlinks, vm),
PathOrFd::Fd(fd) => _fchmod(fd, mode, vm),
OsPathOrFd::Path(path) => _chmod(path, dir_fd, mode, follow_symlinks, vm),
OsPathOrFd::Fd(fd) => _fchmod(fd, mode, vm),
}
}
#[cfg(target_os = "redox")]
#[pyfunction]
fn chmod(
path: PyPathLike,
path: OsPath,
dir_fd: DirFd<0>,
mode: u32,
follow_symlinks: FollowSymlinks,
@@ -836,13 +836,13 @@ pub mod module {
#[cfg(not(target_os = "redox"))]
#[pyfunction]
fn lchmod(path: PyPathLike, mode: u32, vm: &VirtualMachine) -> PyResult<()> {
fn lchmod(path: OsPath, mode: u32, vm: &VirtualMachine) -> PyResult<()> {
_chmod(path, DirFd::default(), mode, FollowSymlinks(false), vm)
}
#[pyfunction]
fn execv(
path: PyPathLike,
path: OsPath,
argv: Either<PyListRef, PyTupleRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
@@ -869,7 +869,7 @@ pub mod module {
#[pyfunction]
fn execve(
path: PyPathLike,
path: OsPath,
argv: Either<PyListRef, PyTupleRef>,
env: PyDictRef,
vm: &VirtualMachine,
@@ -895,8 +895,8 @@ pub mod module {
.into_iter()
.map(|(k, v)| -> PyResult<_> {
let (key, value) = (
PyPathLike::try_from_object(vm, k)?.into_bytes(),
PyPathLike::try_from_object(vm, v)?.into_bytes(),
OsPath::try_from_object(vm, k)?.into_bytes(),
OsPath::try_from_object(vm, v)?.into_bytes(),
);
if memchr::memchr(b'=', &key).is_some() {
@@ -1229,8 +1229,8 @@ pub mod module {
keys.into_iter()
.zip(values.into_iter())
.map(|(k, v)| {
let k = PyPathLike::try_from_object(vm, k)?.into_bytes();
let v = PyPathLike::try_from_object(vm, v)?.into_bytes();
let k = OsPath::try_from_object(vm, k)?.into_bytes();
let v = OsPath::try_from_object(vm, v)?.into_bytes();
if k.contains(&0) {
return Err(
vm.new_value_error("envp dict key cannot contain a nul byte".to_owned())
@@ -1258,9 +1258,9 @@ pub mod module {
#[derive(FromArgs)]
pub(super) struct PosixSpawnArgs {
#[pyarg(positional)]
path: PyPathLike,
path: OsPath,
#[pyarg(positional)]
args: crate::function::ArgIterable<PyPathLike>,
args: crate::function::ArgIterable<OsPath>,
#[pyarg(positional)]
env: crate::function::ArgMapping,
#[pyarg(named, default)]
@@ -1309,7 +1309,7 @@ pub mod module {
let args: crate::function::FuncArgs = args.to_vec().into();
let ret = match id {
PosixSpawnFileActionIdentifier::Open => {
let (fd, path, oflag, mode): (_, PyPathLike, _, _) = args.bind(vm)?;
let (fd, path, oflag, mode): (_, OsPath, _, _) = args.bind(vm)?;
let path = CString::new(path.into_bytes()).map_err(|_| {
vm.new_value_error(
"POSIX_SPAWN_OPEN path should not have nul bytes".to_owned(),
@@ -1846,7 +1846,7 @@ pub mod module {
#[cfg(unix)]
#[pyfunction]
fn pathconf(
path: PathOrFd,
path: OsPathOrFd,
ConfName(name): ConfName,
vm: &VirtualMachine,
) -> PyResult<Option<libc::c_long>> {
@@ -1855,12 +1855,12 @@ pub mod module {
Errno::clear();
debug_assert_eq!(errno::errno(), 0);
let raw = match path {
PathOrFd::Path(path) => {
OsPathOrFd::Path(path) => {
let path = CString::new(path.into_bytes())
.map_err(|_| vm.new_value_error("embedded null character".to_owned()))?;
unsafe { libc::pathconf(path.as_ptr(), name) }
}
PathOrFd::Fd(fd) => unsafe { libc::fpathconf(fd, name) },
OsPathOrFd::Fd(fd) => unsafe { libc::fpathconf(fd, name) },
};
if raw == -1 {
@@ -1876,7 +1876,7 @@ pub mod module {
#[pyfunction]
fn fpathconf(fd: i32, name: ConfName, vm: &VirtualMachine) -> PyResult<Option<libc::c_long>> {
pathconf(PathOrFd::Fd(fd), name, vm)
pathconf(OsPathOrFd::Fd(fd), name, vm)
}
#[pyattr]

View File

@@ -12,7 +12,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
pub(crate) mod module {
use crate::{
builtins::PyStrRef,
stdlib::os::{DirFd, PyPathLike, SupportFunc, TargetIsDirectory, _os},
stdlib::os::{DirFd, OsPath, SupportFunc, TargetIsDirectory, _os},
PyObjectRef, PyResult, VirtualMachine,
};
use std::env;
@@ -25,8 +25,8 @@ pub(crate) mod module {
#[derive(FromArgs)]
#[allow(unused)]
pub(super) struct SymlinkArgs {
src: PyPathLike,
dst: PyPathLike,
src: OsPath,
dst: OsPath,
#[pyarg(flatten)]
_target_is_directory: TargetIsDirectory,
#[pyarg(flatten)]