Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba8b0a7

Browse files
committed
Enhance os._DummyDirEntry
Issue #25911: * Try to fix test_os.BytesWalkTests on Windows * Try to mimick better the reference os.DirEntry on Windows * _DummyDirEntry now caches os.stat() result * _DummyDirEntry constructor now tries to get os.stat()
1 parent 56db16c commit ba8b0a7

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

Lib/os.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,36 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
439439
yield top, dirs, nondirs
440440

441441
class _DummyDirEntry:
442+
"""Dummy implementation of DirEntry
443+
444+
Only used internally by os.walk(bytes). Since os.walk() doesn't need the
445+
follow_symlinks parameter: don't implement it, always follow symbolic
446+
links.
447+
"""
448+
442449
def __init__(self, dir, name):
443450
self.name = name
444451
self.path = path.join(dir, name)
452+
# Mimick FindFirstFile/FindNextFile: we should get file attributes
453+
# while iterating on a directory
454+
self._stat = None
455+
try:
456+
self.stat()
457+
except OSError:
458+
pass
459+
460+
def stat(self):
461+
if self._stat is None:
462+
self._stat = stat(self.path)
463+
return self._stat
445464

446465
def is_dir(self):
447-
return path.isdir(self.path)
466+
stat = self.stat()
467+
return st.S_ISDIR(stat.st_mode)
448468

449469
def is_symlink(self):
450-
return path.islink(self.path)
470+
stat = self.stat()
471+
return st.S_ISLNK(stat.st_mode)
451472

452473
class _dummy_scandir:
453474
# listdir-based implementation for bytes patches on Windows

0 commit comments

Comments
 (0)