Skip to content
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
Next Next commit
is_dir/is_file for windows
  • Loading branch information
youknowone committed Dec 9, 2025
commit d82577a004c7f2ed4ae11c5b0f2adaf3536f5b13
45 changes: 23 additions & 22 deletions crates/vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,17 +507,17 @@ pub(super) mod _os {
Ok(self.mode.process_path(&self.pathval, vm))
}

fn perform_on_metadata(
&self,
follow_symlinks: FollowSymlinks,
action: fn(fs::Metadata) -> bool,
vm: &VirtualMachine,
) -> PyResult<bool> {
#[pymethod]
fn is_dir(&self, follow_symlinks: FollowSymlinks, vm: &VirtualMachine) -> PyResult<bool> {
match super::fs_metadata(&self.pathval, follow_symlinks.0) {
Ok(meta) => Ok(action(meta)),
Ok(meta) => Ok(meta.is_dir()),
Err(e) => {
// FileNotFoundError is caught and not raised
if e.kind() == io::ErrorKind::NotFound {
// On Windows, use cached file_type when file is removed
#[cfg(windows)]
if let Ok(file_type) = &self.file_type {
return Ok(file_type.is_dir());
}
Ok(false)
} else {
Err(e.into_pyexception(vm))
Expand All @@ -526,22 +526,23 @@ pub(super) mod _os {
}
}

#[pymethod]
fn is_dir(&self, follow_symlinks: FollowSymlinks, vm: &VirtualMachine) -> PyResult<bool> {
self.perform_on_metadata(
follow_symlinks,
|meta: fs::Metadata| -> bool { meta.is_dir() },
vm,
)
}

#[pymethod]
fn is_file(&self, follow_symlinks: FollowSymlinks, vm: &VirtualMachine) -> PyResult<bool> {
self.perform_on_metadata(
follow_symlinks,
|meta: fs::Metadata| -> bool { meta.is_file() },
vm,
)
match super::fs_metadata(&self.pathval, follow_symlinks.0) {
Ok(meta) => Ok(meta.is_file()),
Err(e) => {
if e.kind() == io::ErrorKind::NotFound {
// On Windows, use cached file_type when file is removed
#[cfg(windows)]
if let Ok(file_type) = &self.file_type {
return Ok(file_type.is_file());
}
Ok(false)
} else {
Err(e.into_pyexception(vm))
}
}
}
}

#[pymethod]
Expand Down