(original) (raw)
changeset: 100784:2b25fa7e3b7a user: Victor Stinner victor.stinner@gmail.com date: Tue Mar 29 11:25:00 2016 +0200 files: Lib/os.py description: Fix os._DummyDirEntry.is_symlink() Issue #25911: Fix os._DummyDirEntry.is_symlink(), don't follow symbolic links: use os.stat(path, follow_symlinks=False). diff -r 1798a074223a -r 2b25fa7e3b7a Lib/os.py --- a/Lib/os.py Tue Mar 29 09:50:18 2016 +0200 +++ b/Lib/os.py Tue Mar 29 11:25:00 2016 +0200 @@ -452,22 +452,33 @@ # Mimick FindFirstFile/FindNextFile: we should get file attributes # while iterating on a directory self._stat = None + self._lstat = None try: - self.stat() + self.stat(follow_symlinks=False) except OSError: pass - def stat(self): - if self._stat is None: - self._stat = stat(self.path) - return self._stat + def stat(self, *, follow_symlinks=True): + if follow_symlinks: + if self._stat is None: + 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): + 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() return st.S_ISDIR(stat.st_mode) def is_symlink(self): - stat = self.stat() + stat = self.stat(follow_symlinks=False) return st.S_ISLNK(stat.st_mode) class _dummy_scandir: /victor.stinner@gmail.com