pathlib: improve comments on commonpath and bestrelpath
This commit is contained in:
parent
cd67c2a8cf
commit
db08c7fbb0
|
@ -581,7 +581,10 @@ def absolutepath(path: Union[Path, str]) -> Path:
|
||||||
|
|
||||||
def commonpath(path1: Path, path2: Path) -> Optional[Path]:
|
def commonpath(path1: Path, path2: Path) -> Optional[Path]:
|
||||||
"""Return the common part shared with the other path, or None if there is
|
"""Return the common part shared with the other path, or None if there is
|
||||||
no common part."""
|
no common part.
|
||||||
|
|
||||||
|
If one path is relative and one is absolute, returns None.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
return Path(os.path.commonpath((str(path1), str(path2))))
|
return Path(os.path.commonpath((str(path1), str(path2))))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -592,13 +595,17 @@ def bestrelpath(directory: Path, dest: Path) -> str:
|
||||||
"""Return a string which is a relative path from directory to dest such
|
"""Return a string which is a relative path from directory to dest such
|
||||||
that directory/bestrelpath == dest.
|
that directory/bestrelpath == dest.
|
||||||
|
|
||||||
|
The paths must be either both absolute or both relative.
|
||||||
|
|
||||||
If no such path can be determined, returns dest.
|
If no such path can be determined, returns dest.
|
||||||
"""
|
"""
|
||||||
if dest == directory:
|
if dest == directory:
|
||||||
return os.curdir
|
return os.curdir
|
||||||
# Find the longest common directory.
|
# Find the longest common directory.
|
||||||
base = commonpath(directory, dest)
|
base = commonpath(directory, dest)
|
||||||
# Can be the case on Windows.
|
# Can be the case on Windows for two absolute paths on different drives.
|
||||||
|
# Can be the case for two relative paths without common prefix.
|
||||||
|
# Can be the case for a relative path and an absolute path.
|
||||||
if not base:
|
if not base:
|
||||||
return str(dest)
|
return str(dest)
|
||||||
reldirectory = directory.relative_to(base)
|
reldirectory = directory.relative_to(base)
|
||||||
|
|
Loading…
Reference in New Issue