Skip to content

Commit 858a6d0

Browse files
committed
Add DirEntry.{name, path}
1 parent 3828652 commit 858a6d0

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

tests/snippets/stdlib_os.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
5656

5757

5858
FILE_NAME = "test1"
59+
FILE_NAME2 = "test2"
5960
CONTENT = b"testing"
6061
CONTENT2 = b"rustpython"
6162
CONTENT3 = b"BOYA"
@@ -73,3 +74,16 @@ def __exit__(self, exc_type, exc_val, exc_tb):
7374
assert os.read(fd, len(CONTENT2)) == CONTENT2
7475
assert os.read(fd, len(CONTENT3)) == CONTENT3
7576
os.close(fd)
77+
78+
79+
fname2 = tmpdir + os.sep + FILE_NAME2
80+
with open(fname2, "wb"):
81+
pass
82+
files = set()
83+
paths = set()
84+
for dir_entry in os.scandir(tmpdir):
85+
files.add(dir_entry.name)
86+
paths.add(dir_entry.path)
87+
88+
assert files == set([FILE_NAME, FILE_NAME2])
89+
assert paths == set([fname, fname2])

vm/src/stdlib/os.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ impl DirEntryRef {
210210
fn name(self, _vm: &VirtualMachine) -> String {
211211
self.entry.file_name().into_string().unwrap()
212212
}
213+
214+
fn path(self, _vm: &VirtualMachine) -> String {
215+
self.entry.path().to_str().unwrap().to_string()
216+
}
213217
}
214218

215219
#[derive(Debug)]
@@ -269,7 +273,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
269273
});
270274

271275
let dir_entry = py_class!(ctx, "DirEntry", ctx.object(), {
272-
"name" => ctx.new_rustfunc(DirEntryRef::name),
276+
"name" => ctx.new_property(DirEntryRef::name),
277+
"path" => ctx.new_property(DirEntryRef::path),
273278
});
274279

275280
py_module!(vm, "_os", {

0 commit comments

Comments
 (0)