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

Skip to content

Commit 73030df

Browse files
committed
Fix os._DummyDirEntry.is_symlink()
Issue #25911: Fix os._DummyDirEntry.is_symlink(), don't follow symbolic links: use os.stat(path, follow_symlinks=False).
1 parent 80ec58c commit 73030df

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

Lib/os.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,22 +452,33 @@ def __init__(self, dir, name):
452452
# Mimick FindFirstFile/FindNextFile: we should get file attributes
453453
# while iterating on a directory
454454
self._stat = None
455+
self._lstat = None
455456
try:
456-
self.stat()
457+
self.stat(follow_symlinks=False)
457458
except OSError:
458459
pass
459460

460-
def stat(self):
461-
if self._stat is None:
462-
self._stat = stat(self.path)
463-
return self._stat
461+
def stat(self, *, follow_symlinks=True):
462+
if follow_symlinks:
463+
if self._stat is None:
464+
self._stat = stat(self.path)
465+
return self._stat
466+
else:
467+
if self._lstat is None:
468+
self._lstat = stat(self.path, follow_symlinks=False)
469+
return self._lstat
464470

465471
def is_dir(self):
472+
if self._lstat is not None and not self.is_symlink():
473+
# use the cache lstat
474+
stat = self.stat(follow_symlinks=False)
475+
return st.S_ISDIR(stat.st_mode)
476+
466477
stat = self.stat()
467478
return st.S_ISDIR(stat.st_mode)
468479

469480
def is_symlink(self):
470-
stat = self.stat()
481+
stat = self.stat(follow_symlinks=False)
471482
return st.S_ISLNK(stat.st_mode)
472483

473484
class _dummy_scandir:

0 commit comments

Comments
 (0)