Fix os._DummyDirEntry.is_symlink()

Issue #25911: Fix os._DummyDirEntry.is_symlink(), don't follow symbolic links:
use os.stat(path, follow_symlinks=False).
This commit is contained in:
Victor Stinner 2016-03-29 11:25:00 +02:00
parent 80ec58c497
commit 73030df77f
1 changed files with 17 additions and 6 deletions

View File

@ -452,22 +452,33 @@ class _DummyDirEntry:
# Mimick FindFirstFile/FindNextFile: we should get file attributes # Mimick FindFirstFile/FindNextFile: we should get file attributes
# while iterating on a directory # while iterating on a directory
self._stat = None self._stat = None
self._lstat = None
try: try:
self.stat() self.stat(follow_symlinks=False)
except OSError: except OSError:
pass pass
def stat(self): def stat(self, *, follow_symlinks=True):
if self._stat is None: if follow_symlinks:
self._stat = stat(self.path) if self._stat is None:
return self._stat self._stat = stat(self.path)
return self._stat
else:
if self._lstat is None:
self._lstat = stat(self.path, follow_symlinks=False)
return self._lstat
def is_dir(self): def is_dir(self):
if self._lstat is not None and not self.is_symlink():
# use the cache lstat
stat = self.stat(follow_symlinks=False)
return st.S_ISDIR(stat.st_mode)
stat = self.stat() stat = self.stat()
return st.S_ISDIR(stat.st_mode) return st.S_ISDIR(stat.st_mode)
def is_symlink(self): def is_symlink(self):
stat = self.stat() stat = self.stat(follow_symlinks=False)
return st.S_ISLNK(stat.st_mode) return st.S_ISLNK(stat.st_mode)
class _dummy_scandir: class _dummy_scandir: