Skip to content

Commit 1875dd8

Browse files
authored
Propagate Windows path predicate conversion errors (#9569)
* Propagate Windows path predicate conversion errors Suppress ValueError during path conversion while propagating other errors, and preserve false for the invalid -1 descriptor without changing strict descriptor callers. Assisted-by: dot:frontier-intelligence * Use expect_err for invalid borrowed descriptor Satisfy clippy::err_expect while preserving the existing EBADF conversion. Borrowed descriptors implement Debug on all supported and unsupported host targets. Assisted-by: dot:frontier-intelligence
1 parent 702ffe7 commit 1875dd8

2 files changed

Lines changed: 52 additions & 26 deletions

File tree

‎crates/vm/src/ospath_fd.rs‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,38 @@ impl PathConverter {
1616
obj: PyObjectRef,
1717
vm: &VirtualMachine,
1818
) -> PyResult<OsPathOrFd<'fd>> {
19+
self.try_path_or_fd_allow_invalid(obj, vm)?.ok_or_else(|| {
20+
// Preserve the usual EBADF result for the -1 descriptor.
21+
unsafe { crt_fd::Borrowed::try_borrow_raw(-1) }
22+
.expect_err("-1 is not a valid borrowed descriptor")
23+
.into_pyexception(vm)
24+
})
25+
}
26+
27+
/// Convert an invalid -1 descriptor to None for path predicates.
28+
pub(crate) fn try_path_or_fd_allow_invalid<'fd>(
29+
&self,
30+
obj: PyObjectRef,
31+
vm: &VirtualMachine,
32+
) -> PyResult<Option<OsPathOrFd<'fd>>> {
1933
let obj = match self.try_match_str_bytes(obj, vm)? {
20-
Ok(path) => return Ok(OsPathOrFd::Path(path)),
34+
Ok(path) => return Ok(Some(OsPathOrFd::Path(path))),
2135
Err(obj) => obj,
2236
};
2337

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

28-
self.try_pathlike(obj, true, vm).map(OsPathOrFd::Path)
42+
self.try_pathlike(obj, true, vm)
43+
.map(|path| Some(OsPathOrFd::Path(path)))
2944
}
3045

3146
fn try_index_as_fd<'fd>(
3247
&self,
3348
obj: &PyObjectRef,
3449
vm: &VirtualMachine,
35-
) -> Option<PyResult<OsPathOrFd<'fd>>> {
50+
) -> Option<PyResult<Option<OsPathOrFd<'fd>>>> {
3651
let int = obj.try_index_opt(vm)?;
3752
Some((|| {
3853
if obj
@@ -47,8 +62,11 @@ impl PathConverter {
4762
)?;
4863
}
4964
let fd = int?.try_to_primitive(vm)?;
65+
if fd == -1 {
66+
return Ok(None);
67+
}
5068
unsafe { crt_fd::Borrowed::try_borrow_raw(fd) }
51-
.map(OsPathOrFd::Fd)
69+
.map(|fd| Some(OsPathOrFd::Fd(fd)))
5270
.map_err(|e| e.into_pyexception(vm))
5371
})())
5472
}

‎crates/vm/src/stdlib/nt.rs‎

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(crate) mod module {
1212
exceptions::{self, OSErrorBuilder},
1313
function::{ArgMapping, Either, FsPath, OptionalArg},
1414
host_env::crt_fd,
15-
ospath::{OsPath, OsPathOrFd},
15+
ospath::{OsPath, OsPathOrFd, PathConverter},
1616
stdlib::os::{_os, AccessArgs, DirFd, SupportFunc, SymlinkArgs},
1717
};
1818
use core::hint::cold_path;
@@ -242,8 +242,11 @@ pub(crate) mod module {
242242
}
243243

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

@@ -328,45 +331,50 @@ pub(crate) mod module {
328331

329332
// return _testFileType(path, PY_IFDIR)
330333
#[pyfunction]
331-
fn _path_isdir(path: PyObjectRef, vm: &VirtualMachine) -> bool {
332-
PathArg { path }
333-
.to_path_or_fd(vm)
334-
.is_some_and(|p| _test_file_type(&p, PY_IFDIR))
334+
fn _path_isdir(path: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
335+
Ok(PathArg { path }
336+
.to_path_or_fd(vm)?
337+
.is_some_and(|p| _test_file_type(&p, PY_IFDIR)))
335338
}
336339

337340
// return _testFileType(path, PY_IFREG)
338341
#[pyfunction]
339-
fn _path_isfile(args: PathArg, vm: &VirtualMachine) -> bool {
340-
args.to_path_or_fd(vm)
341-
.is_some_and(|p| _test_file_type(&p, PY_IFREG))
342+
fn _path_isfile(args: PathArg, vm: &VirtualMachine) -> PyResult<bool> {
343+
Ok(args
344+
.to_path_or_fd(vm)?
345+
.is_some_and(|p| _test_file_type(&p, PY_IFREG)))
342346
}
343347

344348
// return _testFileType(path, PY_IFLNK)
345349
#[pyfunction]
346-
fn _path_islink(args: PathArg, vm: &VirtualMachine) -> bool {
347-
args.to_path_or_fd(vm)
348-
.is_some_and(|p| _test_file_type(&p, PY_IFLNK))
350+
fn _path_islink(args: PathArg, vm: &VirtualMachine) -> PyResult<bool> {
351+
Ok(args
352+
.to_path_or_fd(vm)?
353+
.is_some_and(|p| _test_file_type(&p, PY_IFLNK)))
349354
}
350355

351356
// return _testFileType(path, PY_IFMNT)
352357
#[pyfunction]
353-
fn _path_isjunction(args: PathArg, vm: &VirtualMachine) -> bool {
354-
args.to_path_or_fd(vm)
355-
.is_some_and(|p| _test_file_type(&p, PY_IFMNT))
358+
fn _path_isjunction(args: PathArg, vm: &VirtualMachine) -> PyResult<bool> {
359+
Ok(args
360+
.to_path_or_fd(vm)?
361+
.is_some_and(|p| _test_file_type(&p, PY_IFMNT)))
356362
}
357363

358364
// return _testFileExists(path, TRUE)
359365
#[pyfunction]
360-
fn _path_exists(args: PathArg, vm: &VirtualMachine) -> bool {
361-
args.to_path_or_fd(vm)
362-
.is_some_and(|p| _test_file_exists(&p, true))
366+
fn _path_exists(args: PathArg, vm: &VirtualMachine) -> PyResult<bool> {
367+
Ok(args
368+
.to_path_or_fd(vm)?
369+
.is_some_and(|p| _test_file_exists(&p, true)))
363370
}
364371

365372
// return _testFileExists(path, FALSE)
366373
#[pyfunction]
367-
fn _path_lexists(args: PathArg, vm: &VirtualMachine) -> bool {
368-
args.to_path_or_fd(vm)
369-
.is_some_and(|p| _test_file_exists(&p, false))
374+
fn _path_lexists(args: PathArg, vm: &VirtualMachine) -> PyResult<bool> {
375+
Ok(args
376+
.to_path_or_fd(vm)?
377+
.is_some_and(|p| _test_file_exists(&p, false)))
370378
}
371379

372380
#[pyfunction]

0 commit comments

Comments
 (0)