Skip to content

Commit 64e6ea0

Browse files
committed
Add DirEntry.{is_file}
1 parent 548f3f3 commit 64e6ea0

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

tests/snippets/stdlib_os.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,16 @@ def __exit__(self, exc_type, exc_val, exc_tb):
8585
names = set()
8686
paths = set()
8787
dirs = set()
88+
files = set()
8889
for dir_entry in os.scandir(tmpdir):
8990
names.add(dir_entry.name)
9091
paths.add(dir_entry.path)
9192
if dir_entry.is_dir():
9293
dirs.add(dir_entry.name)
94+
if dir_entry.is_file():
95+
files.add(dir_entry.name)
9396

9497
assert names == set([FILE_NAME, FILE_NAME2, FOLDER])
9598
assert paths == set([fname, fname2, folder])
9699
assert dirs == set([FOLDER])
100+
assert files == set([FILE_NAME, FILE_NAME2])

vm/src/stdlib/os.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ impl DirEntryRef {
222222
.map_err(|s| vm.new_os_error(s.to_string()))?
223223
.is_dir())
224224
}
225+
226+
fn is_file(self, vm: &VirtualMachine) -> PyResult<bool> {
227+
Ok(self
228+
.entry
229+
.file_type()
230+
.map_err(|s| vm.new_os_error(s.to_string()))?
231+
.is_file())
232+
}
225233
}
226234

227235
#[derive(Debug)]
@@ -284,6 +292,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
284292
"name" => ctx.new_property(DirEntryRef::name),
285293
"path" => ctx.new_property(DirEntryRef::path),
286294
"is_dir" => ctx.new_rustfunc(DirEntryRef::is_dir),
295+
"is_file" => ctx.new_rustfunc(DirEntryRef::is_file),
287296
});
288297

289298
py_module!(vm, "_os", {

0 commit comments

Comments
 (0)