GH-105793: Add follow_symlinks argument to pathlib.Path.is_dir() and is_file() (GH-105794)

Brings `pathlib.Path.is_dir()` and `in line with `os.DirEntry.is_dir()`, which
will be important for implementing generic path walking and globbing.
Likewise `is_file()`.
This commit is contained in:
Barney Gale 2023-06-26 17:58:17 +01:00 committed by GitHub
parent 5d4dbf0e30
commit 219effa876
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 18 deletions

View file

@ -817,12 +817,12 @@ class Path(PurePath):
return False
return True
def is_dir(self):
def is_dir(self, *, follow_symlinks=True):
"""
Whether this path is a directory.
"""
try:
return S_ISDIR(self.stat().st_mode)
return S_ISDIR(self.stat(follow_symlinks=follow_symlinks).st_mode)
except OSError as e:
if not _ignore_error(e):
raise
@ -833,13 +833,13 @@ class Path(PurePath):
# Non-encodable path
return False
def is_file(self):
def is_file(self, *, follow_symlinks=True):
"""
Whether this path is a regular file (also True for symlinks pointing
to regular files).
"""
try:
return S_ISREG(self.stat().st_mode)
return S_ISREG(self.stat(follow_symlinks=follow_symlinks).st_mode)
except OSError as e:
if not _ignore_error(e):
raise