mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
bpo-33721: Make some os.path functions and pathlib.Path methods be tolerant to invalid paths. (#7695)
Such functions as os.path.exists(), os.path.lexists(), os.path.isdir(), os.path.isfile(), os.path.islink(), and os.path.ismount() now return False instead of raising ValueError or its subclasses UnicodeEncodeError and UnicodeDecodeError for paths that contain characters or bytes unrepresentative at the OS level.
This commit is contained in:
parent
7bdf28265a
commit
0185f34ddc
15 changed files with 181 additions and 54 deletions
|
@ -17,7 +17,7 @@ def exists(path):
|
|||
"""Test whether a path exists. Returns False for broken symbolic links"""
|
||||
try:
|
||||
os.stat(path)
|
||||
except OSError:
|
||||
except (OSError, ValueError):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
@ -28,7 +28,7 @@ def isfile(path):
|
|||
"""Test whether a path is a regular file"""
|
||||
try:
|
||||
st = os.stat(path)
|
||||
except OSError:
|
||||
except (OSError, ValueError):
|
||||
return False
|
||||
return stat.S_ISREG(st.st_mode)
|
||||
|
||||
|
@ -40,7 +40,7 @@ def isdir(s):
|
|||
"""Return true if the pathname refers to an existing directory."""
|
||||
try:
|
||||
st = os.stat(s)
|
||||
except OSError:
|
||||
except (OSError, ValueError):
|
||||
return False
|
||||
return stat.S_ISDIR(st.st_mode)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue