GH-101357: Suppress OSError from pathlib.Path.exists() and is_*() (#118243)

Suppress all `OSError` exceptions from `pathlib.Path.exists()` and `is_*()`
rather than a selection of more common errors as we do presently. Also
adjust the implementations to call `os.path.exists()` etc, which are much
faster on Windows thanks to GH-101196.
This commit is contained in:
Barney Gale 2024-05-14 18:53:15 +01:00 committed by GitHub
parent d8e0e00919
commit fbe6a0988f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 92 additions and 125 deletions

View file

@ -502,12 +502,46 @@ class Path(PathBase, PurePath):
"""
return os.stat(self, follow_symlinks=follow_symlinks)
def exists(self, *, follow_symlinks=True):
"""
Whether this path exists.
This method normally follows symlinks; to check whether a symlink exists,
add the argument follow_symlinks=False.
"""
if follow_symlinks:
return os.path.exists(self)
return os.path.lexists(self)
def is_dir(self, *, follow_symlinks=True):
"""
Whether this path is a directory.
"""
if follow_symlinks:
return os.path.isdir(self)
return PathBase.is_dir(self, follow_symlinks=follow_symlinks)
def is_file(self, *, follow_symlinks=True):
"""
Whether this path is a regular file (also True for symlinks pointing
to regular files).
"""
if follow_symlinks:
return os.path.isfile(self)
return PathBase.is_file(self, follow_symlinks=follow_symlinks)
def is_mount(self):
"""
Check if this path is a mount point
"""
return os.path.ismount(self)
def is_symlink(self):
"""
Whether this path is a symbolic link.
"""
return os.path.islink(self)
def is_junction(self):
"""
Whether this path is a junction.