Skip to content

Commit c9c9800

Browse files
committed
is_dir/is_file for windows
1 parent cc534d2 commit c9c9800

File tree

1 file changed

+23
-22
lines changed
  • crates/vm/src/stdlib

1 file changed

+23
-22
lines changed

crates/vm/src/stdlib/os.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -507,17 +507,17 @@ pub(super) mod _os {
507507
Ok(self.mode.process_path(&self.pathval, vm))
508508
}
509509

510-
fn perform_on_metadata(
511-
&self,
512-
follow_symlinks: FollowSymlinks,
513-
action: fn(fs::Metadata) -> bool,
514-
vm: &VirtualMachine,
515-
) -> PyResult<bool> {
510+
#[pymethod]
511+
fn is_dir(&self, follow_symlinks: FollowSymlinks, vm: &VirtualMachine) -> PyResult<bool> {
516512
match super::fs_metadata(&self.pathval, follow_symlinks.0) {
517-
Ok(meta) => Ok(action(meta)),
513+
Ok(meta) => Ok(meta.is_dir()),
518514
Err(e) => {
519-
// FileNotFoundError is caught and not raised
520515
if e.kind() == io::ErrorKind::NotFound {
516+
// On Windows, use cached file_type when file is removed
517+
#[cfg(windows)]
518+
if let Ok(file_type) = &self.file_type {
519+
return Ok(file_type.is_dir());
520+
}
521521
Ok(false)
522522
} else {
523523
Err(e.into_pyexception(vm))
@@ -526,22 +526,23 @@ pub(super) mod _os {
526526
}
527527
}
528528

529-
#[pymethod]
530-
fn is_dir(&self, follow_symlinks: FollowSymlinks, vm: &VirtualMachine) -> PyResult<bool> {
531-
self.perform_on_metadata(
532-
follow_symlinks,
533-
|meta: fs::Metadata| -> bool { meta.is_dir() },
534-
vm,
535-
)
536-
}
537-
538529
#[pymethod]
539530
fn is_file(&self, follow_symlinks: FollowSymlinks, vm: &VirtualMachine) -> PyResult<bool> {
540-
self.perform_on_metadata(
541-
follow_symlinks,
542-
|meta: fs::Metadata| -> bool { meta.is_file() },
543-
vm,
544-
)
531+
match super::fs_metadata(&self.pathval, follow_symlinks.0) {
532+
Ok(meta) => Ok(meta.is_file()),
533+
Err(e) => {
534+
if e.kind() == io::ErrorKind::NotFound {
535+
// On Windows, use cached file_type when file is removed
536+
#[cfg(windows)]
537+
if let Ok(file_type) = &self.file_type {
538+
return Ok(file_type.is_file());
539+
}
540+
Ok(false)
541+
} else {
542+
Err(e.into_pyexception(vm))
543+
}
544+
}
545+
}
545546
}
546547

547548
#[pymethod]

0 commit comments

Comments
 (0)