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:
Serhiy Storchaka 2018-09-18 11:28:51 +03:00 committed by GitHub
parent 7bdf28265a
commit 0185f34ddc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 181 additions and 54 deletions

View file

@ -1331,6 +1331,9 @@ def exists(self):
if e.errno not in _IGNORED_ERROS:
raise
return False
except ValueError:
# Non-encodable path
return False
return True
def is_dir(self):
@ -1345,6 +1348,9 @@ def is_dir(self):
# Path doesn't exist or is a broken symlink
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
return False
except ValueError:
# Non-encodable path
return False
def is_file(self):
"""
@ -1359,6 +1365,9 @@ def is_file(self):
# Path doesn't exist or is a broken symlink
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
return False
except ValueError:
# Non-encodable path
return False
def is_mount(self):
"""
@ -1392,6 +1401,9 @@ def is_symlink(self):
raise
# Path doesn't exist
return False
except ValueError:
# Non-encodable path
return False
def is_block_device(self):
"""
@ -1405,6 +1417,9 @@ def is_block_device(self):
# Path doesn't exist or is a broken symlink
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
return False
except ValueError:
# Non-encodable path
return False
def is_char_device(self):
"""
@ -1418,6 +1433,9 @@ def is_char_device(self):
# Path doesn't exist or is a broken symlink
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
return False
except ValueError:
# Non-encodable path
return False
def is_fifo(self):
"""
@ -1431,6 +1449,9 @@ def is_fifo(self):
# Path doesn't exist or is a broken symlink
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
return False
except ValueError:
# Non-encodable path
return False
def is_socket(self):
"""
@ -1444,6 +1465,9 @@ def is_socket(self):
# Path doesn't exist or is a broken symlink
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
return False
except ValueError:
# Non-encodable path
return False
def expanduser(self):
""" Return a new path with expanded ~ and ~user constructs