Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify patch a bit
  • Loading branch information
barneygale committed Feb 9, 2025
commit ba4d393e547dc8555c4e9e42ee9eea3af45009c1
68 changes: 37 additions & 31 deletions Lib/pathlib/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def _ensure_different_file(self, source):
raise err


class _PosixPathInfo:
class _PathInfoBase:
__slots__ = ('_path', '_stat_result', '_lstat_result')

def __init__(self, path):
Expand Down Expand Up @@ -417,34 +417,6 @@ def _stat(self, *, follow_symlinks=True):
self._lstat_result = None
return self._lstat_result

def exists(self, *, follow_symlinks=True):
"""Whether this path exists."""
st = self._stat(follow_symlinks=follow_symlinks)
if st is None:
return False
return True

def is_dir(self, *, follow_symlinks=True):
"""Whether this path is a directory."""
st = self._stat(follow_symlinks=follow_symlinks)
if st is None:
return False
return S_ISDIR(st.st_mode)

def is_file(self, *, follow_symlinks=True):
"""Whether this path is a regular file."""
st = self._stat(follow_symlinks=follow_symlinks)
if st is None:
return False
return S_ISREG(st.st_mode)

def is_symlink(self):
"""Whether this path is a symbolic link."""
st = self._stat(follow_symlinks=False)
if st is None:
return False
return S_ISLNK(st.st_mode)

def _posix_permissions(self, *, follow_symlinks=True):
"""Return the POSIX file permissions, or None if stat() fails."""
st = self._stat(follow_symlinks=follow_symlinks)
Expand Down Expand Up @@ -488,7 +460,7 @@ def _xattrs(self, *, follow_symlinks=True):
return None


class _WindowsPathInfo(_PosixPathInfo):
class _WindowsPathInfo(_PathInfoBase):
"""Implementation of pathlib.types.PathInfo that provides status
information for Windows paths. Don't try to construct it yourself."""
__slots__ = ('_exists', '_is_dir', '_is_file', '_is_symlink')
Comment thread
barneygale marked this conversation as resolved.
Expand Down Expand Up @@ -544,10 +516,44 @@ def is_symlink(self):
return self._is_symlink


class _PosixPathInfo(_PathInfoBase):
"""Implementation of pathlib.types.PathInfo that provides status
information for POSIX paths. Don't try to construct it yourself."""
__slots__ = ()

def exists(self, *, follow_symlinks=True):
"""Whether this path exists."""
st = self._stat(follow_symlinks=follow_symlinks)
if st is None:
return False
return True

def is_dir(self, *, follow_symlinks=True):
"""Whether this path is a directory."""
st = self._stat(follow_symlinks=follow_symlinks)
if st is None:
return False
return S_ISDIR(st.st_mode)

def is_file(self, *, follow_symlinks=True):
"""Whether this path is a regular file."""
st = self._stat(follow_symlinks=follow_symlinks)
if st is None:
return False
return S_ISREG(st.st_mode)

def is_symlink(self):
"""Whether this path is a symbolic link."""
st = self._stat(follow_symlinks=False)
if st is None:
return False
return S_ISLNK(st.st_mode)


PathInfo = _WindowsPathInfo if os.name == 'nt' else _PosixPathInfo


class DirEntryInfo(_PosixPathInfo):
class DirEntryInfo(_PathInfoBase):
"""Implementation of pathlib.types.PathInfo that provides status
information by querying a wrapped os.DirEntry object. Don't try to
construct it yourself."""
Expand Down