gh-117607: Speedup os.path.relpath() (GH-117608)

This commit is contained in:
Nice Zombies 2024-05-01 23:44:55 +02:00 committed by GitHub
parent 424438b11e
commit a7711a2a4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 12 deletions

View file

@ -532,15 +532,17 @@ def relpath(path, start=None):
start = os.fspath(start)
try:
start_list = [x for x in abspath(start).split(sep) if x]
path_list = [x for x in abspath(path).split(sep) if x]
start_tail = abspath(start).lstrip(sep)
path_tail = abspath(path).lstrip(sep)
start_list = start_tail.split(sep) if start_tail else []
path_list = path_tail.split(sep) if path_tail else []
# Work out how much of the filepath is shared by start and path.
i = len(commonprefix([start_list, path_list]))
rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
if not rel_list:
return curdir
return join(*rel_list)
return sep.join(rel_list)
except (TypeError, AttributeError, BytesWarning, DeprecationWarning):
genericpath._check_arg_types('relpath', path, start)
raise