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
26 changes: 22 additions & 4 deletions crates/vm/src/ospath_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,38 @@ impl PathConverter {
obj: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<OsPathOrFd<'fd>> {
self.try_path_or_fd_allow_invalid(obj, vm)?.ok_or_else(|| {
// Preserve the usual EBADF result for the -1 descriptor.
unsafe { crt_fd::Borrowed::try_borrow_raw(-1) }
.expect_err("-1 is not a valid borrowed descriptor")
.into_pyexception(vm)
})
}

/// Convert an invalid -1 descriptor to None for path predicates.
pub(crate) fn try_path_or_fd_allow_invalid<'fd>(
&self,
obj: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<Option<OsPathOrFd<'fd>>> {
let obj = match self.try_match_str_bytes(obj, vm)? {
Ok(path) => return Ok(OsPathOrFd::Path(path)),
Ok(path) => return Ok(Some(OsPathOrFd::Path(path))),
Err(obj) => obj,
};

if let Some(fd) = self.try_index_as_fd(&obj, vm) {
return fd;
}

self.try_pathlike(obj, true, vm).map(OsPathOrFd::Path)
self.try_pathlike(obj, true, vm)
.map(|path| Some(OsPathOrFd::Path(path)))
}

fn try_index_as_fd<'fd>(
&self,
obj: &PyObjectRef,
vm: &VirtualMachine,
) -> Option<PyResult<OsPathOrFd<'fd>>> {
) -> Option<PyResult<Option<OsPathOrFd<'fd>>>> {
let int = obj.try_index_opt(vm)?;
Some((|| {
if obj
Expand All @@ -47,8 +62,11 @@ impl PathConverter {
)?;
}
let fd = int?.try_to_primitive(vm)?;
if fd == -1 {
return Ok(None);
}
unsafe { crt_fd::Borrowed::try_borrow_raw(fd) }
.map(OsPathOrFd::Fd)
.map(|fd| Some(OsPathOrFd::Fd(fd)))
.map_err(|e| e.into_pyexception(vm))
})())
}
Expand Down
52 changes: 30 additions & 22 deletions crates/vm/src/stdlib/nt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) mod module {
exceptions::{self, OSErrorBuilder},
function::{ArgMapping, Either, FsPath, OptionalArg},
host_env::crt_fd,
ospath::{OsPath, OsPathOrFd},
ospath::{OsPath, OsPathOrFd, PathConverter},
stdlib::os::{_os, AccessArgs, DirFd, SupportFunc, SymlinkArgs},
};
use core::hint::cold_path;
Expand Down Expand Up @@ -242,8 +242,11 @@ pub(crate) mod module {
}

impl PathArg {
fn to_path_or_fd(&self, vm: &VirtualMachine) -> Option<OsPathOrFd<'static>> {
OsPathOrFd::try_from_object(vm, self.path.clone()).ok()
fn to_path_or_fd(&self, vm: &VirtualMachine) -> PyResult<Option<OsPathOrFd<'static>>> {
match PathConverter::new().try_path_or_fd_allow_invalid(self.path.clone(), vm) {
Err(err) if err.fast_isinstance(vm.ctx.exceptions.value_error) => Ok(None),
result => result,
}
}
}

Expand Down Expand Up @@ -328,45 +331,50 @@ pub(crate) mod module {

// return _testFileType(path, PY_IFDIR)
#[pyfunction]
fn _path_isdir(path: PyObjectRef, vm: &VirtualMachine) -> bool {
PathArg { path }
.to_path_or_fd(vm)
.is_some_and(|p| _test_file_type(&p, PY_IFDIR))
fn _path_isdir(path: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
Ok(PathArg { path }
.to_path_or_fd(vm)?
.is_some_and(|p| _test_file_type(&p, PY_IFDIR)))
}

// return _testFileType(path, PY_IFREG)
#[pyfunction]
fn _path_isfile(args: PathArg, vm: &VirtualMachine) -> bool {
args.to_path_or_fd(vm)
.is_some_and(|p| _test_file_type(&p, PY_IFREG))
fn _path_isfile(args: PathArg, vm: &VirtualMachine) -> PyResult<bool> {
Ok(args
.to_path_or_fd(vm)?
.is_some_and(|p| _test_file_type(&p, PY_IFREG)))
}

// return _testFileType(path, PY_IFLNK)
#[pyfunction]
fn _path_islink(args: PathArg, vm: &VirtualMachine) -> bool {
args.to_path_or_fd(vm)
.is_some_and(|p| _test_file_type(&p, PY_IFLNK))
fn _path_islink(args: PathArg, vm: &VirtualMachine) -> PyResult<bool> {
Ok(args
.to_path_or_fd(vm)?
.is_some_and(|p| _test_file_type(&p, PY_IFLNK)))
}

// return _testFileType(path, PY_IFMNT)
#[pyfunction]
fn _path_isjunction(args: PathArg, vm: &VirtualMachine) -> bool {
args.to_path_or_fd(vm)
.is_some_and(|p| _test_file_type(&p, PY_IFMNT))
fn _path_isjunction(args: PathArg, vm: &VirtualMachine) -> PyResult<bool> {
Ok(args
.to_path_or_fd(vm)?
.is_some_and(|p| _test_file_type(&p, PY_IFMNT)))
}

// return _testFileExists(path, TRUE)
#[pyfunction]
fn _path_exists(args: PathArg, vm: &VirtualMachine) -> bool {
args.to_path_or_fd(vm)
.is_some_and(|p| _test_file_exists(&p, true))
fn _path_exists(args: PathArg, vm: &VirtualMachine) -> PyResult<bool> {
Ok(args
.to_path_or_fd(vm)?
.is_some_and(|p| _test_file_exists(&p, true)))
}

// return _testFileExists(path, FALSE)
#[pyfunction]
fn _path_lexists(args: PathArg, vm: &VirtualMachine) -> bool {
args.to_path_or_fd(vm)
.is_some_and(|p| _test_file_exists(&p, false))
fn _path_lexists(args: PathArg, vm: &VirtualMachine) -> PyResult<bool> {
Ok(args
.to_path_or_fd(vm)?
.is_some_and(|p| _test_file_exists(&p, false)))
}

#[pyfunction]
Expand Down
Loading